operator<< and namespace??

Discussion in 'C++' started by Rene Ivon Shamberger, Dec 4, 2012.

  1. I have the class below that overloads the extractor operator, however, Visual Studio 10 complains giving me an error. I know why the error is created, I think, but I don't know how to solve it.
    The problem is that the operator<< method is inside a namespace (ABC), I think that that is the problem.
    How can I solve this issue? or, am I looking at the wrong thing here?


    #include <iostream>
    namespace ABC{
    class SomeClass{
    std::string str;
    public:
    SomeClass() { str = "Text";}
    virtual ~SomeClass();
    std::string& getData(){return str;}
    friend std::eek:stream& operator<<( std::eek:stream& os,
    ABC::SomeClass obj );
    }; // class
    } //namespace


    std::eek:stream& operator<<( std::eek:stream& os, ABC::SomeClass obj ) {
    return os << obj.getData() << std::endl;
    }

    1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl
    *** HERE IS THE PROBLEM ***
    ABC::eek:perator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,
    class ABC::SomeClass)" (??6jme@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@VAddress@0@@Z)
    referenced in function "void __cdecl testAddress(void)" (?testAddress@@YAXXZ)
    Rene Ivon Shamberger, Dec 4, 2012
    #1
    1. Advertising

  2. Rene Ivon Shamberger

    Ian Collins Guest

    Rene Ivon Shamberger wrote:

    ** Please wrap your lines! **

    > I have the class below that overloads the extractor operator, however, Visual Studio 10 complains giving me an error. I know why the error is created, I think, but I don't know how to solve it.
    > The problem is that the operator<< method is inside a namespace (ABC), I think that that is the problem.
    > How can I solve this issue? or, am I looking at the wrong thing here?
    >
    >
    > #include <iostream>
    > namespace ABC{
    > class SomeClass{
    > std::string str;
    > public:
    > SomeClass() { str = "Text";}
    > virtual ~SomeClass();
    > std::string& getData(){return str;}
    > friend std::eek:stream& operator<<( std::eek:stream& os,
    > ABC::SomeClass obj );
    > }; // class
    > } //namespace
    >
    >
    > std::eek:stream& operator<<( std::eek:stream& os, ABC::SomeClass obj ) {
    > return os << obj.getData() << std::endl;
    > }
    >
    > 1>main.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl
    > *** HERE IS THE PROBLEM ***
    > ABC::eek:perator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,
    > class ABC::SomeClass)" (??6jme@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@VAddress@0@@Z)
    > referenced in function "void __cdecl testAddress(void)" (?testAddress@@YAXXZ


    The operator definition has to be in the namespace.

    --
    Ian Collins
    Ian Collins, Dec 4, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    552
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,106
  3. Anonymous
    Replies:
    3
    Views:
    509
    Ron Natalie
    Aug 18, 2003
  4. Jason Heyes
    Replies:
    1
    Views:
    435
    Woebegone
    Nov 19, 2004
  5. mrstephengross
    Replies:
    3
    Views:
    387
    James Kanze
    May 10, 2007
Loading...

Share This Page