![]() |
operator<< and namespace??
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::ostream& operator<<( std::ostream& os, ABC::SomeClass obj ); }; // class } //namespace std::ostream& operator<<( std::ostream& 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::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &, class ABC::SomeClass)" (??6jme@@YAAAV?$basic_ostream@DU?$char_traits@D@st d@@@std@@AAV12@VAddress@0@@Z) referenced in function "void __cdecl testAddress(void)" (?testAddress@@YAXXZ) |
Re: operator<< and namespace??
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::ostream& operator<<( std::ostream& os, > ABC::SomeClass obj ); > }; // class > } //namespace > > > std::ostream& operator<<( std::ostream& 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::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &, > class ABC::SomeClass)" (??6jme@@YAAAV?$basic_ostream@DU?$char_traits@D@st d@@@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 |
| All times are GMT. The time now is 08:51 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.