Original Post
I'm writing a class and am overloading some of the operators. The problem is that if I put the class definition in a header file and the actual code for the overloaded operator function I get errors. But as soon as I move the code back to the header file (inside the class definition) it all works just fine. I'm using Visual C++ 7 and can't for the life of me not figure out why the exact same code works in the header file but not in the cpp file. Thanks in advance for any replies. (code shortened for brevity) It works when done like this: I get errors when I try this: The errors I get are the following (all for the line marked with (*) above in code.cpp)
// code.h
namespace MyNS
{
class MyClass
{
MyClass &operator=( const MyClass &T )
{
// code here
}
};
}
// code.h
namespace MyNS
{
class MyClass
{
MyClass &operator=( const MyClass &T );
};
}
// code.cpp
MyClass &MyNS::MyClass::operator=( const MyClass &T ) // (*)
{
// code
}
code.cpp(251): error C2143: syntax error : missing ';' before '&'
code.cpp(251): error C2501: 'MyClass' : missing storage-class or type specifiers
code.cpp(252): error C2501: 'MyNS::MyClass::operator`='' : missing storage-class or type specifiers
code.cpp(252): error C2556: 'int &MyNS::MyClass::operator =(const int)' : overloaded function differs only by return type from 'MyNS::MyClass &MyNS::MyClass::operator =(const int)'
code.h(56) : see declaration of 'MyNS::MyClass::operator`=''
code.cpp(252): error C2371: 'MyNS::MyClass::operator`='' : redefinition; different basic types
code.h(56) : see declaration of 'MyNS::MyClass::operator`=''