Skip to main content
gamedev.net
🔒 Locked

Another operator overloading problem

Started by Mutare 1 reply 500+ views
Original Post
Mutare
Mutare
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:

// code.h

namespace MyNS
{

class MyClass
{
MyClass &operator=( const MyClass &T )
{
// code here
}

};

}

I get errors when I try this:

// code.h
namespace MyNS
{

class MyClass
{
MyClass &operator=( const MyClass &T );

};

}


// code.cpp

MyClass &MyNS::MyClass::operator=( const MyClass &T ) // (*)
{
// code
}

The errors I get are the following (all for the line marked with (*) above in code.cpp)

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`=''

-------------------------------------------------"The object of war is not to die for your countrybut to make the other bastard die for his."
Mutare
Mutare
Thank you very much!! That worked :)
I would never had figured that out myself, I had put the code in the header file. A little strange behaviour this I think, in my eyes it should be the same.. oh well.

Once again, thank you :)
-------------------------------------------------"The object of war is not to die for your countrybut to make the other bastard die for his."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.