stringstream, istream, conversions

M

morgan

#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#define re 1 //realis
#define im 2 //imaginalis

typedef int complex;

complex F1 (complex x[2],complex y[2]){
cout<<"Dodawanie liczb zespolonych"<<
"("<<x[re]<<"+"<<x[im]<<"i)+"<<"("<<y[re]<<"+"<<y[im]<<"i)=";
complex z[2];
z[re]=x[re]+y[re];
z[im]=x[im]+y[re];
return z;} // <========Here



void main()
{clrscr();
complex x[2],y[2];
cout<<"Podaj liczbe rzeczywista do pierwszej liczby zespolnoej x+yi x= ";
cin>>x[re];
cout<<"Podaj liczbe urojon¡ do pierwszej liczby zespolnoej x+yi y= ";
cin>>x[im];
cout<<"Podaj liczbe rzeczywista do drugiej liczby zespolnoej x+yi x= ";
cin>>y[re];
cout<<"Podaj liczbe urojon¡ do drugiej liczby zespolnoej x+yi y= ";
cin>>y[im];
complex z[2];
z=F1(x,y); // <========And here


cout<<"("<<z[re]<<"+"<<z[im]<<"i)"<<endl;
getchar();
}
 
S

SerGioGio

Hello,

I have problems understanding why the following example does not compile in
VC7.1:

std::stringstream test;
const std::istream& test2 = test; //OK
const std::istream& test3 = std::stringstream(); // fails

Error is:
'std::basic_stringstream<_Elem,_Traits,_Alloc>::__ctor' : no non-explicit
constructor available for implicit conversion

I used to think test + test2 lines and test3 lines are equivalent but it
seems not. Any ideas?
Note: I know that test3 line will lead to undefined behaviour, but I would
like to know why it does not compile?

Thanks in advance,

SerGioGioGio
 
K

Karl Heinz Buchegger

The simple answer is:
you can't return an array. Just as you cannot assign
an array:

int i[5], j[5];

...
i = j; // won't work. You cannot assign arrays.


In your case a simple solution would be to *not*
use an array, but use a struct instead:

struct complex
{
int re;
int im;
};

complex F1 (complex x, complex y )
{
cout << "Dodawanie liczb zespolonych" <<
"(" << x.re << "+" << x.im << "i)+" <<
"(" << y.re << "+" << y.im << "i)=";

complex z;

z.re = x.re + y.re;
z.im = x.im + y.re;
return z;
}
void main()
int main()
{
complex x, y;
cout << "Podaj liczbe rzeczywista do pierwszej liczby zespolnoej x+yi x= ";
cin >> x.re;
cout << "Podaj liczbe urojon¡ do pierwszej liczby zespolnoej x+yi y= ";
cin >> x.im;
cout << "Podaj liczbe rzeczywista do drugiej liczby zespolnoej x+yi x= ";
cin >> y.re;
cout << "Podaj liczbe urojon¡ do drugiej liczby zespolnoej x+yi y= ";
cin >> y.im;

complex z;
z = F1( x, y );

cout << "(" << z.re << "+" << z.im << "i)" << endl;
getchar();
}
 
T

Tom Widmer

Hello,

I have problems understanding why the following example does not compile in
VC7.1:

std::stringstream test;
const std::istream& test2 = test; //OK
const std::istream& test3 = std::stringstream(); // fails

Error is:
'std::basic_stringstream<_Elem,_Traits,_Alloc>::__ctor' : no non-explicit
constructor available for implicit conversion

I used to think test + test2 lines and test3 lines are equivalent but it
seems not. Any ideas?
Note: I know that test3 line will lead to undefined behaviour, but I would
like to know why it does not compile?

You cannot bind a temporary to a non-const reference. This is to
prevent code like this compiling:

void f(int& i)
{
i = 10;
}

int main()
{
double d = 0;
f(d); //temporary int created and modified
//here d is still 0!
}

It is legal to bind a temporary to a const reference, since that can't
have the problem above.

It has been suggested that the rule would be better if it were: "You
can't bind a temporary to a non-const reference *unless binding can be
done without any implicit conversions*." Or something like that.

Tom
 
M

Mike Wahler

SerGioGio said:
Hello,

I have problems understanding why the following example does not compile in
VC7.1:

std::stringstream test;
const std::istream& test2 = test; //OK

Why 'const'. You won't be able the modify the stream
(i.e. use it to extract characters).
const std::istream& test3 = std::stringstream(); // fails

Error is:
'std::basic_stringstream<_Elem,_Traits,_Alloc>::__ctor' : no non-explicit
constructor available for implicit conversion

This answers your last question below.
I used to think test + test2 lines and test3 lines are equivalent but it
seems not. Any ideas?
Note: I know that test3 line will lead to undefined behaviour, but I would
like to know why it does not compile?

Thanks in advance,

Also note that streams are not copyable. (Well you *can* copy them,
but it's a convoluted process (See e.g. Josuttis), and not recommended.

What specifically are you trying to do?

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top