Jump to content
  • Advertisement
Sign in to follow this  
nossrannug

Problem overloading operator -

This topic is 2989 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
How does it not work? What is the result? Also, are you sure your constructor is working correctly?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!