I have several problems to declare, define, and use the following operator in many
.cpp files :
std::ostream & operator << (std::ostream &, const MyClassA &);
I succeed to declare and define this operator for my class MyClassA, defined in MyClassA.cpp.
I declared my class as following :
// MyClassA.h
class MyClassA;
std::ostream & operator << (std::ostream &, const MyClassA &);
class MyClassA {
public:
// constructors, destructor
// overloading ostream operator <<
friend std::ostream & operator << (std::ostream& dest, const MyClassA& obj);
private:
// members
};
The definition of this operator doesn't raise any compilation error.
I included this header in the cpp file MyclassB.cpp.
The MyClassB class contains a method that calls the operator as following :
//definition header of the method
void MyClassB::aMethod(...) {
// uses an MyClassA object
MyClassA obj(...);
// displays information contained in the object
cout << obj; // here is the problem : compilation errors
// end of the definition
}
I included all possible standard headers in my cpp files (iostream.h, string, sstream,
iomanip.h, windows.h) .
I tried all combinaisons of 'using namespace std;' and '#include <...>' .
I always get compilation errors.
Do I have to declare the operator << using 'std::operator << '
Is it a problem dealing with namespace linking
Do I need to add compilation options ( I use the default options)

Problem overloading and using ostream operator << for my own classes
UncleRemus
#include <iosfwd>
in your header file, and I would absolutely remove the
using namespace std;
at least until you're sure you have everything correct. I put together some simple code that I think duplicates what you're trying to do in VS 2005, and was able to get it to work.
Stampc
I removed all occurences of 'using namespace std::', added 'std::' when errors appeared.
But it doesn't solve the problem. Everything remains unchanged.
I guess that the problem deals with the templates, but I don't see what happens.
Thank you very much for your help.
Miguel Angel Zavala Vizcaya
Streamable.h
#include
<iosfwd>class streamable;
std::ostream & operator << (std::ostream &, const streamable &);
class streamable
{
public:
streamable(void);
};
Streamable.cpp
#include
"streamable.h"#include <ostream>
streamable::streamable(
void){
}
std::ostream &
operator << (std::ostream &ost, const streamable &s){
ost << "Hello from a streamable!";
return ost;
}
Test.cpp
#include
"streamable.h"#include <iostream>
int
main(array<System::String ^> ^args){
streamable s;
std::cout << s; return 0;
}
DGardner
Chia Li
I created the following C++ Win 32 Console project in VS 2005 Pro :
#include
"stdafx.h"#include
<iostream>using
namespace std; class MyClassA{
public:
// ...
friend std::ostream & operator << (std::ostream& dest, const MyClassA& obj);
private:
// ...
}; class MyClassB
{
void aMethod()
{
MyClassA obj;
cout << obj;
}
};
int
_tmain(int argc, _TCHAR* argv[]) { return 0; }The compilation is done without errors.
I only added the following 2 lines :
#include <iostream>
using namespace std;
Hope that helps.
jmd
Charlie C
The example doesn't work, but I suppose that my compiler version is too old to support the expected functionalities. I shall wait for a newer version of visual c++ ( and a professionnal one).
Dome C.
Thanks,
Ayman Shoukry
VC++ Team
pkirangi
If I define the operator << without std:: , I get no compilation error for the .cpp file where the definition is, but I get the C2679 error (No operator defined ... or no
acceptable conversion for the MyClassA type) for the line that calls the operator.
I also get an error when I call the operator with the full syntax.
I got a clue : The method where I call the overloaded operator, declared with no std:: prefix, doesn't compile a 'cout << str;' line, where str is a string object,
though I included all file headers for the string handling : string.h and sstream
I use the 'using namespace std;' just before I include the file headers for my classes. Is there something wrong with the template headers
If I define the operator << with std:: , as in :
std::ostream & std::operator << (std::ostream & dest, const MyClassA & obj);
then I can't compile the file where the definition is, because I see the following errors in the file header:
C2244 : 'std::<<' unable to resolve function overload
C2245 : 'std::<<' nonexistent function specified as friend
I know that some implementations need the std:: prefix for this operator.
Should I use any namespace for the declaration/definition headers
Did I forget any keyword
I wanted to define the overloaded operator in a separate module (.obj file) that
can be used in other modules. Do I have to release the definition in a header file, as I have to do for templates In this case, do I need a specific header extension, as for the sstream file
I have compiled this files at warning level 3 (standard options)
gul007
The problem occurs when the operator is declared in a header file (MyclassA.h), defined in one file (MyClassA.cpp), and used in an other .cpp file by an other class (MyclassB in MyClassB.cpp).
Can you understand my problem
Thank you for your help.
JamesinSC
Thanks,
Ayman Shoukry
VC++ Team