473,699 Members | 2,364 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 7733
#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
3341
by: Marc Schellens | last post by:
class myClass { stringstream ioss; istream* is; void f() { .... string initStr("");
4
5809
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
10882
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
12243
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
5697
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
4203
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
9184
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
1959
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
7857
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
8687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8617
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
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8884
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
7751
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
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
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
3
2009
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.