0
class Vector{
......
.......
private:
int dim;
public:
int getDim() {
return this->dim;


}
const Vector operator+(const  Vector& right){
this->getSize();
}
 };

And I got compile error in this->getSize();. It is caused fact, that argument right is const. I don't know where is problem. I don't try modify right.

1 Answer 1

3

Presumably you have a non-const method Vector::getSize(). You need to make it const so that it can be called on const objects or via const references or pointers to const. For example:

int getSize() const;
              ^^^^^

Also note that it doesn't make much sense to return a const value (and would inhibit move semantics if you had them). The canonical form of an addition member operator would be

// const method: A = B + C should not modify B
Vector operator+(const Vector& right) const;
                                      ^^^^^

and the non-member

Vector operator+(const Vector& left, const Vector& right);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.