operator<< and namespace??

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

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)
 
I

Ian Collins

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,053
Messages
2,570,431
Members
47,075
Latest member
TysonV438

Latest Threads

Top