Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   operator<< and namespace?? (http://www.velocityreviews.com/forums/t955089-operator-and-namespace.html)

Rene Ivon Shamberger 12-04-2012 02:20 AM

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)

Ian Collins 12-04-2012 04:44 AM

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 09:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.