overloading operator -

Hi all.

I just started learning c++ and its been going really well.
But now i've run into a problem i can't find the solution for.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//not gona write the whole code. just the problem

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

private:
    int x;
    int y;
    int z;

//then i got:
T1 operator -(const T1& a, const T1& 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.


Is there anyone here that can tell me why: aObj = T1(10,0,0) - T1(0,0,5); dosnt work? (or just give me a hint since its homework :P )

edit: should have added that if I comment out the line that dosnt work everything compiles and runs no problem. but when i try and compile with the line it says: '-' : undeclared identifier
Last edited on
Sorry, can't help you there. Looks perfectly fine to me, I can compile it and the operator returns the right results. Your problem either lies elsewhere in your code, or it's your compiler's fault.
Yeah, i couldnt see the problem with it.
I'm using Visual Studio 2010 professional, maby thats the problem :P gona see if I cant find my VS2008 CD and try it there.
Found out what the problem was.
The teacher wrote tha main.cpp file, but the text was in a pdf file. I copied the content from there.
So all I did to fix this was delete the - and then type it again and it works :P
Topic archived. No new replies allowed.