By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Question about ostream

P: n/a

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::ostream' to 'std::ostream'

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
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* 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::ostream' to 'std::ostream'

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::ostream class disables copying (no
copy constructor), and you'll have to pass that stream by reference.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a

"Robert Sherry" <rs******@comcast.net> wrote in message
news:_L********************@comcast.com...

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 );
void func1(ostream& os1);

main()
int main()
{
func1( cout );
}

void
func1( ostream os1 )

void func1(ostream& os1) {
}

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #3

P: n/a
Alf P. Steinbach wrote:
You're trying to _copy_ a stream.

That doesn't make sense.

Because it doesn't make sense the std::ostream 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.

--
Ney André de Mello Zunino
Jul 23 '05 #4

P: n/a

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

Robert Sherry

"Sumit Rajan" <su*********@gmail.com> wrote in message
news:3a*************@individual.net...

"Robert Sherry" <rs******@comcast.net> wrote in message
news:_L********************@comcast.com...

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 );


void func1(ostream& os1);

main()


int main()
{
func1( cout );
}

void
func1( ostream os1 )

void func1(ostream& os1)
{
}

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>

Jul 23 '05 #5

P: n/a
Alf P. Steinbach wrote:
* 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::ostream' to 'std::ostream'

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::ostream 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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #6

P: n/a
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

Jul 23 '05 #7

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.