Cant use cout in G++

F

Fozya

Hi,

I have problems compiling some code that perfectly runs in VS.Net in
the G++. Here is what I got:

MyClass a;
MyClass b;

//assign values to a and b
.......

//print the sum of those objects
cout << "the sum is " << (a + b) << endl;

It work WS.Net fine but in G++ i have to write like this:

MyClass temp;
temp = a + b;
cout << "the sum is " << temp << endl;

The overloaded + operator has this signature and returns a copy of new
object.

MyClass operator +( const MyClass &a);

Thanks, in advance!

P.S. These are the error messages that I get

distance.cpp:218:2: warning: no newline at end of file
In file included from dist_test.cpp:3:
distance.h:72:7: warning: no newline at end of file
dist_test.cpp: In function `int main()':
dist_test.cpp:65: no match for `std::basic_ostream<char,
std::char_traits said:
& << Distance' operator
/usr/include/c++/3.2.2/bits/ostream.tcc:55: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:77:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_ios<_CharT,
_Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT =
char, _Traits
= std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:99:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::ios_base&(*)(std::ios_base&)) [with
_CharT = char,
_Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:171:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:208:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(long unsigned int) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:146:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(bool) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:104:
std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::eek:perator<<(short
int) [with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:115:
std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::eek:perator<<(short
unsigned
int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:119:
std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::eek:perator<<(int)
[with _CharT
= char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:130:
std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::eek:perator<<(unsigned
int)
[with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:234:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(long long int) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:272:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(long long unsigned int) [with _CharT = char,
_Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:298:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:145:
std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT, _Traits>::eek:perator<<(float)
[with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:323:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(long double) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:348:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(const void*) [with _CharT = char, _Traits =
std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:120:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::eek:perator<<(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT =
char, _Traits = std::char_traits<char>]
distance.h:69: std::eek:stream& operator<<(std::eek:stream&,
Distance&)
/usr/include/c++/3.2.2/ostream:251:
std::basic_ostream<char,
_Traits>& std::eek:perator<<(std::basic_ostream<char, _Traits>&, const
unsigned
char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:246:
std::basic_ostream<char,
_Traits>& std::eek:perator<<(std::basic_ostream<char, _Traits>&, const
signed
char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:622:
std::basic_ostream<char, _Traits>&
std::eek:perator<<(std::basic_ostream<char,
_Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:572:
std::basic_ostream<_CharT, _Traits>&
std::eek:perator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
[with
_CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:227:
std::basic_ostream<char,
_Traits>& std::eek:perator<<(std::basic_ostream<char, _Traits>&,
unsigned char)
[with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:222:
std::basic_ostream<char,
_Traits>& std::eek:perator<<(std::basic_ostream<char, _Traits>&,
signed char)
[with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:500:
std::basic_ostream<char, _Traits>&
std::eek:perator<<(std::basic_ostream<char,
_Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/include/c++/3.2.2/ostream:211:
std::basic_ostream<_CharT,
_Traits>& std::eek:perator<<(std::basic_ostream<_CharT, _Traits>&,
char) [with
_CharT = char, _Traits = std::char_traits<char>]
dist_test.cpp:111:15: warning: no newline at end of file
 
M

Mike Wahler

Fozya said:
Hi,

I have problems compiling some code that perfectly runs in VS.Net in
the G++. Here is what I got:

MyClass a;
MyClass b;

//assign values to a and b
......

//print the sum of those objects
cout << "the sum is " << (a + b) << endl;

It work WS.Net fine but in G++ i have to write like this:

MyClass temp;
temp = a + b;
cout << "the sum is " << temp << endl;

The overloaded + operator has this signature and returns a copy of new
object.

MyClass operator +( const MyClass &a);

Thanks, in advance!

P.S. These are the error messages that I get

We cannot hope to diagnose only fragments of code.
Please provide a complete program which demonstrates
the problem. Also when quoting error messages,
mark each line which is specified by them
with a comment.

-Mike
 
R

Rob Williscroft

Fozya wrote in
MyClass a;
MyClass b;

//assign values to a and b
......

//print the sum of those objects
cout << "the sum is " << (a + b) << endl;

It work WS.Net fine but in G++ i have to write like this:

MyClass temp;
temp = a + b;
cout << "the sum is " << temp << endl;

The overloaded + operator has this signature and returns a copy of new
object.

MyClass operator +( const MyClass &a);

That should really be:

MyClass operator +( MyClass const &a) const;
or
friend MyClass operator +( MyClass const &lhs, MyClass const &rhs );

Note that the error messages you gave indicate that gcc can't find
a sutible operator << for (a + b) but since you say it can compile
with cout << temp then I can have a wild guess:

You have declared your operator << like so:

std::eek:stream &operator << ( std::eek:stream &os, MyClass & mc );

If this is correct then change it to:

std::eek:stream &operator << ( std::eek:stream &os, MyClass const & mc );

(note the "const") or if MyClass is sutible for pass by value:

std::eek:stream &operator << ( std::eek:stream &os, MyClass mc );

All of this is because in standard c++ you cant "bind" a temporary
(the result of (a + b)) to a non-const reference (MS allow it as
an extention).

Also use cl's (VS.NET's c++ compiler) /Za option in future to disable
MS "extentions". Note that without this option g++ (at least 3.2) is
defenetly a more conforming compiler, unfortunatly with it you can't
include <windows.h> :(.

HTH

Rob.
 
F

Fozya

Mike Wahler said:
We cannot hope to diagnose only fragments of code.
Please provide a complete program which demonstrates
the problem. Also when quoting error messages,
mark each line which is specified by them
with a comment.

-Mike

Here is the complete code. Its still not the original code but I have
isolated the problem which I still having with this code. I compile it
by typing:

g++ MyClass.cpp main.cpp

Also, its hard for me to comment on the error messages since I have no
idea what they are. Yesterday it was a first day I've used g++.
Actually, I use VS.Net and only make sure my compilies in g++ as well.

Thank you,

Fozya

MyClass.h:


#ifndef _MyClass_H_
#define _MyClass_H_

#include <iostream>

using namespace std;

class MyClass
{
public:

explicit MyClass(int data = 0):mData(data){}

ostream & Print(ostream &out);

MyClass operator + (MyClass &mc)const;


private:

long mData;
};

ostream & operator << (ostream &out, MyClass &mc);


#endif


MyClass.cpp:

#include "MyClass.h"
#include <iostream>

using namespace std;

ostream & MyClass::print(ostream &out)
{
out << this->mData;
return out;
}

MyClass MyClass::eek:perator + (MyClass &mc)const
{
return MyClass(mData + mc.mData);
}


ostream & operator << (ostream &out, MyClass &mc)
{
return mc.Print(out);
}

main.cpp:


#include <iostream>
#include "MyClass.h"

using namespace std;


int main()
{

MyClass a(5);
MyClass b(6);

cout << "The sum is " << a + b << endl;



return 0;
}
 
R

Ron Natalie

Fozya said:
#ifndef _MyClass_H_
#define _MyClass_H_

You are not permitted to use symbols like this in your code. Symbols with
leading underscore and followed by an upper-case letter are reserved for
the implementation.
#include <iostream>

using namespace std;

Do not put using directives in include files you expect others to have to use.
You can pollute your own namespace this way if you like, but others should
not be suprised by having the std namespace dumped into their programs
just because they included your header.
ostream & Print(ostream &out);

You want to make this a const method.
MyClass operator + (MyClass &mc)const;

Probalby want to make the argument const as well.
ostream & operator << (ostream &out, MyClass &mc);

Make MyClass argument const.
int main()
{

MyClass a(5);
MyClass b(6);

cout << "The sum is " << a + b << endl;


This fails because the result of a+b is an rvalue (temproary), and you can't bind it to
a non-const reference. Fix the const correctness of the various functions above and
this should work.
 
F

Fozya

Rob Williscroft said:
Fozya wrote in

That should really be:

MyClass operator +( MyClass const &a) const;
or
friend MyClass operator +( MyClass const &lhs, MyClass const &rhs );

Note that the error messages you gave indicate that gcc can't find
a sutible operator << for (a + b) but since you say it can compile
with cout << temp then I can have a wild guess:

You have declared your operator << like so:

std::eek:stream &operator << ( std::eek:stream &os, MyClass & mc );

If this is correct then change it to:

std::eek:stream &operator << ( std::eek:stream &os, MyClass const & mc );

(note the "const") or if MyClass is sutible for pass by value:

std::eek:stream &operator << ( std::eek:stream &os, MyClass mc );

All of this is because in standard c++ you cant "bind" a temporary
(the result of (a + b)) to a non-const reference (MS allow it as
an extention).

Also use cl's (VS.NET's c++ compiler) /Za option in future to disable
MS "extentions". Note that without this option g++ (at least 3.2) is
defenetly a more conforming compiler, unfortunatly with it you can't
include <windows.h> :(.

HTH

Rob.

Rob, you are the man!! Not using const was exactly my problem. I've
posted the complete code earlier which didn't show up yet. But what I
have to do is pass my object as a const ref to the << operator. Then
inside the << I would have to call 'Print' first and then return
'out'.

Thanks a lot to everyone who takes time to answer questions here!
Google groups is the best Q&A resource I ever used. I hope I'll be
able to contribute one of these days.

Fozya
 
F

Fozya

Ron Natalie said:
You are not permitted to use symbols like this in your code. Symbols with
leading underscore and followed by an upper-case letter are reserved for
the implementation.

Do not put using directives in include files you expect others to have to use.
You can pollute your own namespace this way if you like, but others should
not be suprised by having the std namespace dumped into their programs
just because they included your header.

Interesting. I usually use a combination of some of these: class name,
lib name, date, version, and my name with underscores only in the
middle for that define statement. The above example I took from class
notes so I guess my professor is being sloppy.

Now, about 'using". Do I still need it in .cpp file? Is it better to
include any libraries I need in .cpp file or .h file? For example,
where <iostream> should be included for MyClass?

Thanks,

Fozya
 

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
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top