473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Cant use cout in G++

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<char>
& << Distance' operator

/usr/include/c++/3.2.2/bits/ostream.tcc:55: candidates are:
std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(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>::operator<<(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>::operator<<(std::ios_base&(*)(std::ios_ba se&)) [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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(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>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with
_CharT =
char, _Traits = std::char_traits<char>]
distance.h:69: std::ostream& operator<<(std::ostream&,
Distance&)
/usr/include/c++/3.2.2/ostream:251:
std::basic_ostream<char,
_Traits>& std::operator<<(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::operator<<(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::operator<<(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::operator<<(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::operator<<(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::operator<<(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::operator<<(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::operator<<(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
Jul 19 '05 #1
6 7561
"Fozya" <al***********@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
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
Jul 19 '05 #2
Fozya wrote in news:9c**************************@posting.google.c om:

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::ostream &operator << ( std::ostream &os, MyClass & mc );

If this is correct then change it to:

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

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

std::ostream &operator << ( std::ostream &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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3
"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<3b*****************@newsread3.news.pas.earth link.net>...
"Fozya" <al***********@hotmail.com> wrote in message
news:9c**************************@posting.google.c om...
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


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::operator + (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;
}
Jul 19 '05 #4

"Fozya" <al***********@hotmail.com> wrote in message news:9c**************************@posting.google.c om...

#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.
Jul 19 '05 #5
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.204>...
Fozya wrote in news:9c**************************@posting.google.c om:

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::ostream &operator << ( std::ostream &os, MyClass & mc );

If this is correct then change it to:

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

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

std::ostream &operator << ( std::ostream &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
Jul 19 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f*********************@news.newshosting.com >...
"Fozya" <al***********@hotmail.com> wrote in message news:9c**************************@posting.google.c om...

#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.


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
Jul 19 '05 #7

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

Similar topics

4
by: Rich | last post by:
Hi all I am using DEVCPP 4.9.8.1 which uses MingW32 GCC 3.2 compiler I am trying to compile this program, but without success, I am sure the compiler is set up ok, as I can compile and run...
0
by: shan | last post by:
#include<fstream> #include<iostream> #include<string> #include<iomanip> #include<stdlib.h> #include<conio.h> #include<stdio.h> using namespace std; class library
4
by: roxorsoxor2345 | last post by:
I have this custom string class that I have created. #include <cstdlib> #include <iostream> using namespace std; class Cstring { private:
5
by: Ouwet5775 | last post by:
Hello peeps. Well i have tried runing the folwoing program several times, and i cant figure out what i am doing wrong. The goal of this is to input values for an array, find the maximum and the...
2
by: andrewanderson | last post by:
hi can anyone help me with this prog. cant find the prob why it cant display cout<<"This is the display of your transaction"<<endl; ifstream fobj; //declare input file stream ...
6
by: ALi Shaikh | last post by:
When ever I put in the % symbol it gives me stupid errors but if I change the % to a - or +,*,/ it works here is the code. .. //Price of Pizza's #include<iostream.h> void main() { float...
5
gpraghuram
by: gpraghuram | last post by:
HI all, I wrote the following piece of code class A { A(){cout<<"ctor"<<endl; ~A(){cout<<"dtor"<<endl; }; int main()
4
by: rez151 | last post by:
trying to create a tree of hash nodes, with each node having a max of 5 objects, and a maximum of 5 child nodes...we insert 6 sucesfully and "explode" to create 3 child nodes, but when it gets to 7...
1
by: qwert7465 | last post by:
here is my code. i am only getting one error: // Final Project #include <iostream.h> #include <iomanip.h> void checkout(); void start_or_startover();
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.