Question about ostream

  • Thread starter =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
  • Start date
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Alf said:
You're trying to _copy_ a stream.

That doesn't make sense.

Because it doesn't make sense the std::eek:stream class disables copying (no
copy constructor), and you'll have to pass that stream by reference.

Indeed and I would expect that compiler's diagnostic to be more clear
about it. The error message given is quite misleading, if not nonsensical.
 
R

Robert Sherry

It is my understanding that the standard variable cout is of type
ostream. Please consider the following C++ program.

#include <iostream>

using namespace std;

void func1( ostream os1 );

main()
{
func1( cout );
}

void
func1( ostream os1 )
{
}

When I compile this program using the Microsoft Visual C++ Studio, the
compiler generates an error on the call to func1. The message is:
c:\dev\ostreamTest\main.cpp(9): error C2664: 'func1' : cannot
convert parameter 1 from 'std::eek:stream' to 'std::eek:stream'

I find it hard to believe that I have found a bug in the compiler but I do
not know what is wrong. Since the types of the formal and actual parameters
are the same, why is any conversion needed? I am hoping for some help from
the group.

Robert Sherry
 
A

Alf P. Steinbach

* Robert Sherry:
It is my understanding that the standard variable cout is of type
ostream. Please consider the following C++ program.

#include <iostream>

using namespace std;

void func1( ostream os1 );

main()
{
func1( cout );
}

void
func1( ostream os1 )
{
}

When I compile this program using the Microsoft Visual C++ Studio, the
compiler generates an error on the call to func1. The message is:
c:\dev\ostreamTest\main.cpp(9): error C2664: 'func1' : cannot
convert parameter 1 from 'std::eek:stream' to 'std::eek:stream'

I find it hard to believe that I have found a bug in the compiler but I do
not know what is wrong. Since the types of the formal and actual parameters
are the same, why is any conversion needed? I am hoping for some help from
the group.

You're trying to _copy_ a stream.

That doesn't make sense.

Because it doesn't make sense the std::eek:stream class disables copying (no
copy constructor), and you'll have to pass that stream by reference.
 
R

Robert Sherry

I wish to thank the group for their fast response. It was very helpful.

Robert Sherry
 
A

Artie Gold

Alf said:
* Robert Sherry:



You're trying to _copy_ a stream.

That doesn't make sense.

Because it doesn't make sense the std::eek:stream class disables copying (no
copy constructor), and you'll have to pass that stream by reference.
Just to make things a little more clear, make that:

void func1( ostream& os1 );

and

void func1( ostream& os1 ) {}

The terminology we usually use, i.e. `pass it by reference' must be
terribly confusing for C++ newbies; passing by value and passing by
reference are indistinguishable at the function call site. Perhaps we
should start saying that the variable should be `received by reference'
instead?

Just a thought.

Cheers,
--ag
 
L

lixiaoyao

when I compile this code,it generate the following rsult,I am a little
bit wondering.
[library]$ g++ ofstream.cpp&
"[4] 2864"
why generate this?
thanks
 
A

Alf P. Steinbach

* lixiaoyao:
when I compile this code,it generate the following rsult,I am a little
bit wondering.
[library]$ g++ ofstream.cpp&
"[4] 2864"
why generate this?

The Unix shell is off-topic in this group.
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top