Public Group

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

## 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 on other sites
How does it not work? What is the result? Also, are you sure your constructor is working correctly?

##### 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 on other sites
Post the actual code that demonstrates the problem.

##### 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

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633352
• Total Posts
3011483
• ### Who's Online (See full list)

There are no registered users currently online

×