473,748 Members | 3,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:21 8: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:6 5: no match for `std::basic_ost ream<char,
std::char_trait s<char>
& << Distance' operator

/usr/include/c++/3.2.2/bits/ostream.tcc:55: candidates are:
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::basi c_ostream<_Char T,
_Traits>&(*)(st d::basic_ostrea m<_CharT, _Traits>&)) [with _CharT =
char,
_Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:77:
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::basi c_ios<_CharT,
_Traits>&(*)(st d::basic_ios<_C harT, _Traits>&)) [with _CharT =
char, _Traits
= std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:99:
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::ios_ base&(*)(std::i os_base&)) [with
_CharT = char,
_Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:171 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(long int) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:208 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(long unsigned int) [with _CharT = char,
_Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:146 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(bool) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:104:
std::basic_ostr eam<_CharT,
_Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(short
int) [with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:115:
std::basic_ostr eam<_CharT,
_Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(short
unsigned
int) [with _CharT = char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:119:
std::basic_ostr eam<_CharT,
_Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(int)
[with _CharT
= char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:130:
std::basic_ostr eam<_CharT,
_Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(unsigned
int)
[with _CharT = char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:234 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(long long int) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:272 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(long long unsigned int) [with _CharT = char,
_Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:298 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(double) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:145:
std::basic_ostr eam<_CharT,
_Traits>& std::basic_ostr eam<_CharT, _Traits>::opera tor<<(float)
[with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:323 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(long double) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:348 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(const void*) [with _CharT = char, _Traits =
std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:120 :
std::basic_ostr eam<_CharT, _Traits>& std::basic_ostr eam<_CharT,
_Traits>::opera tor<<(std::basi c_streambuf<_Ch arT, _Traits>*) [with
_CharT =
char, _Traits = std::char_trait s<char>]
distance.h:69: std::ostream& operator<<(std: :ostream&,
Distance&)
/usr/include/c++/3.2.2/ostream:251:
std::basic_ostr eam<char,
_Traits>& std::operator<< (std::basic_ost ream<char, _Traits>&, const
unsigned
char*) [with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:246:
std::basic_ostr eam<char,
_Traits>& std::operator<< (std::basic_ost ream<char, _Traits>&, const
signed
char*) [with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:622 :
std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char,
_Traits>&, const char*) [with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:572 :
std::basic_ostr eam<_CharT, _Traits>&
std::operator<< (std::basic_ost ream<_CharT, _Traits>&, const char*)
[with
_CharT = char, _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:227:
std::basic_ostr eam<char,
_Traits>& std::operator<< (std::basic_ost ream<char, _Traits>&,
unsigned char)
[with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:222:
std::basic_ostr eam<char,
_Traits>& std::operator<< (std::basic_ost ream<char, _Traits>&,
signed char)
[with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/bits/ostream.tcc:500 :
std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char,
_Traits>&, char) [with _Traits = std::char_trait s<char>]
/usr/include/c++/3.2.2/ostream:211:
std::basic_ostr eam<_CharT,
_Traits>& std::operator<< (std::basic_ost ream<_CharT, _Traits>&,
char) [with
_CharT = char, _Traits = std::char_trait s<char>]
dist_test.cpp:1 11:15: warning: no newline at end of file
Jul 19 '05 #1
6 7582
"Fozya" <al***********@ hotmail.com> wrote in message
news:9c******** *************** ***@posting.goo gle.com...
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.goo gle.com:

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******@mkwah ler.net> wrote in message news:<3b******* **********@news read3.news.pas. earthlink.net>. ..
"Fozya" <al***********@ hotmail.com> wrote in message
news:9c******** *************** ***@posting.goo gle.com...
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::operat or + (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.goo gle.com...

#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.RE MOVE.co.uk> wrote in message news:<Xn******* *************** ************@19 5.129.110.204>. ..
Fozya wrote in news:9c******** *************** ***@posting.goo gle.com:

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.newshostin g.com>...
"Fozya" <al***********@ hotmail.com> wrote in message news:9c******** *************** ***@posting.goo gle.com...

#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
2404
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 hello.cpp fine here is the listing please can someone tell what I am doing wrong please #include <iostream>
0
1959
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
2814
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
1652
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 do a linear search for a given number. But i cant seem to input the number anywhere. Any kind of input would help me out alot. Thanks ;)
2
2086
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 fobj.open("trans.txt"); //open file if(!fobj) //File not opened
6
1955
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 inch; float fin; cout<<endl;
5
1350
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
1534
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 our temp node has null pointers??? DRIVER PROGRAM:: #include <iostream> #include "MLH.h" using std::cout;
1
1456
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
8991
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
8830
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
9370
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8242
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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
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.