473,748 Members | 6,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stringstream, istream, conversions

Hello,

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

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

Error is:
'std::basic_str ingstream<_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
Jul 22 '05 #1
4 7742
#include<conio. h>
#include<stdio. h>
#include<iostre am.h>
#define re 1 //realis
#define im 2 //imaginalis

typedef int complex;

complex F1 (complex x[2],complex y[2]){
cout<<"Dodawani e 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();
}
Jul 22 '05 #2
morgan wrote:

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();
}
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #3
On Tue, 14 Dec 2004 08:35:55 +0100, "SerGioGio" <se*******@yaho o.fr>
wrote:
Hello,

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

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

Error is:
'std::basic_st ringstream<_Ele m,_Traits,_Allo c>::__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
Jul 22 '05 #4

"SerGioGio" <se*******@yaho o.fr> wrote in message
news:41******** @news.starhub.n et.sg...
Hello,

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

std::stringstre am 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::stringstre am(); // fails

Error is:
'std::basic_str ingstream<_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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3344
by: Marc Schellens | last post by:
class myClass { stringstream ioss; istream* is; void f() { .... string initStr("");
4
5814
by: David Johnstone | last post by:
Hi Group, I am using gcc 3.2.2 on linux 2.4.19-4GB (SuSe 8.2). I have a situation where I need to tie an ostringstream to an istringstream so I can write to one and read back what I wrote from the other. I had expected the code to look like: ostringstream* ostr_stdin = new ostringstream();
5
10887
by: cherico | last post by:
I'd like to read stings from cin and put them in stringstream. I use a string object as an intermediate "container" to store data from cin and then put them in stringstream. stringstream ss ; string str ; cin >> str ; ss << str ;
2
12252
by: Ney André de Mello Zunino | last post by:
Hello. Is g++ 3.3.3 (Cygwin) correct in rejecting the following test program? #include <istream> #include <sstream> #include <fstream> void test(std::istream& source) {
3
5700
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a binary file through a unix pipe to the program (cat file | prog) everything works fine. However, when I feed data from a program, one that connects to an open socket so that multiple files can be processed, across a unix pipe (prog1 | prog) the...
5
4215
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
9
9193
by: Àî°× | last post by:
hi all: I want erase a stringstream' content for input new data to it. example: std::stringstream stm; stm<<"this is a string"; std::cout<<stm.str(); // here print:this is a string
2
1962
by: blackstreetcat | last post by:
I'm trying to send a stringstream to a function in a dll like so: in Dll: class A {int i; f(istream& in) { in>>i; } };
2
7867
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.