Problem overloading and using ostream operator << for my own classes

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)



Answer this question

Problem overloading and using ostream operator << for my own classes

  • jgd12345

    This is what I wrote:

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


  • scotcurry

    There is no compilation error if the operator is used in the same .cpp file, as mentioned.
    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.

  • graey

    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)


  • Mr. Mort

    Could you please post the exact error your are seeing

    Thanks,
      Ayman Shoukry
      VC++ Team

  • Shelly Guo - MSFT

    std::operator<< is wrong, so you can take the std:: part out.  Make sure you have

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

  • Steven Wilmot

    Thank you for everything.

    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). Smile

  • barty72

    I'm using vc++ 6.0 introducy edition + sp5 + processor pack.
  • lextm

    Which compiler version are you using

    Thanks,
      Ayman Shoukry
      VC++ Team

  • Npotnis

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


  • Bak2DFuture

    I'm very sorry.
    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.

  • Problem overloading and using ostream operator << for my own classes