Skip to main content
gamedev.net
🔒 Locked

Problem overloading operator -

Started by nossrannug Apr 16, 2010 at 3:44 PM 3 replies 600+ views
Original Post
nossrannug
nossrannug
Hello all. I'm currently trying to learn Cplusplus (wont let me do the plus sign :S ) and its been going really well so far. But now it seems i've run into a problem that i can't figure out. I'm overloading operators and the problem is with the operator -. I've done:

private:
    int x;
    int y;
    int z;

public:
friend T1 operator -(const  T1& a, const T2& b);

//Then:
T1 operator -(const  T1& a, const T2& b)
{
    T1 temp;
    temp.x = a.x - b.x;
    temp.y = a.y - b.y;
    temp.z = a.z - b.z;
    return temp;
}

//Then in my main function I've got:
aObj = aObj - T1(0,0,10);  //this works.

aObj = T1(10,0,0) - T1(0,0,5);  //this dosn't work.

anyone here that can tell me why aObj = T1(10,0,0) - T1(0,0,5); dosn't work?
Ariste
Ariste
How does it not work? What is the result? Also, are you sure your constructor is working correctly?
nossrannug
nossrannug
What I do is cout << aObj.
That prints out x, y, z to the screen.

if I comment out the line that dosnt work, everything compiles and runs no problem.

if i try to compile with the line that gives error it wont compile.
it says: '-' : undeclared identifier
jpetrie
jpetrie
Post the actual code that demonstrates the problem.
nossrannug
nossrannug
Found out what the problem was. I got the content of the main.cpp file from the teacher in a pdf file. So I just copied the text from there and pasted into VS2010. All I had to do to fix the problem was delete the - and retype it. :P

Topic Locked

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

Sign in to reply to this topic.