class point
{
public:
inline const point operator +(const point &p) const;
inline const point operator -() const;
double x, y, z;
};
inline const point point::operator +(const point &p) const
{
point result;
result.x = this->x + p.x;
result.y = this->y + p.y;
result.z = this->z + p.z;
return result;
}
const member function depreciated....
i have the following code
For some reason if I do the following code I get errors in DevC++ talking about depricated values
point a, b;
point &func(const point &a, const point &b)
{
point result = a + b;
return result;
}
I get an error for the line "point result = a + b"
Why can I not add 'a' and 'b'. I know they are constants, but is there a way to get around this?
I think the problem is not adding the two values, but storing the result.
The operator+ returns a 'const point', but result is defined to be 'point'.
The operator+ returns a 'const point', but result is defined to be 'point'.
Posting the exact error would be helpful. I'm going to have to disagree with nmi about the const: I've started labeling all return types const since they should be and I've had no such trouble.
Quote:The operator+ returns a 'const point', but result is defined to be 'point'.
Irrelivant. You are simply missing an operator =. You should also have a copy constructor.
Here's the exact error from DevC++
189 C:\Documents and Settings\Sean\Desktop\main.cpp passing `const point' as `this' argument of `point point3::operator+(const point&)' discards qualifiers
189 C:\Documents and Settings\Sean\Desktop\main.cpp passing `const point' as `this' argument of `point point3::operator+(const point&)' discards qualifiers
Quote:Original post by DeyjaQuote:The operator+ returns a 'const point', but result is defined to be 'point'.
Irrelivant. You are simply missing an operator =. You should also have a copy constructor.
Irrelivant. The compile will generate a default assignment operator and copy constructor that performs a shallow copy.
Quote:Original post by oconnellseanm
Here's the exact error from DevC++
189 C:\Documents and Settings\Sean\Desktop\main.cpp passing `const point' as `this' argument of `point point3::operator+(const point&)' discards qualifiers
What's the point3?
Irrelevant. You guys were spelling it wrong. Also, compiler copy constructors don't ever work right in my experience...so I agree with Mr. Copy Constructor over there.
I tried to compile the code with DJGPP and got this result:
where line 22 contains:
That is because the reference returned will point to the local variable that does not exist anymore when returned.
You should better define func to return a point instead of a point&.
C:\TEMP>gpp -O -o point.exe point.ccpoint.cc: In function `point& func(const point&, const point&)':point.cc:22: warning: reference to local variable `result' returned
where line 22 contains:
point result = a + b;
That is because the reference returned will point to the local variable that does not exist anymore when returned.
You should better define func to return a point instead of a point&.
Quote:Original post by oconnellseanmFirst off, defining the return type of your addition and subtraction overloads as a const non-reference means that they can not be chained (a + b + c - d would fail to compile).
i have the following code
The rest is a type mismatch.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement