简体   繁体   中英

non-member function operator+(…) cannot have cv-qualifier

I am currently writing a program to simulate gas in a box. But I have come across an issue with class operators. Unfortunately to make things annoying the class is called Vector, I know some won't like it being called this.

Vector.h

class Vector {
private:

double x;
double y;
double z;

public:

Vector & operator+=(const Vector & v){
    x += v.x;
    y += v.y;
    z += v.z;
    return *this;
}

I have editted the code above to make this problem clearer. I have left out the constructer etc as they dont effect the problem.

And for .cpp

Vector.cpp

Vector operator+(const Vector v) const{
Vector v2(*this);
v2+=v;
return v2;
}

And his yeilds error:

C:\\Users\\XXXXX\\ClionProjects\\XXXXX\\YYYYY\\Vector.cpp:95:34: error: non-member function 'Vector operator+(Vector)' cannot have cv-qualifier

C:\\Users\\XXXXX\\ClionProjects\\XXXXX\\YYYYY\\Vector.cpp: In function 'Vector operator+(Vector)':

Any help greatly appreciated.

The error tells you what's wrong. You made these non-member functions, but they should be member functions. The compiler detected the problem by your (correct) use of const , though the number of arguments is also lacking and that would be a further compilation error once the first was fixed.

Either you forgot to put these function definitions inside the class Vector { ... }; area, or you forgot to write Vector:: before operator .

Only member functions can have const specifiers. Both your operators are non-member functions (because they are missing class name as part of their signature) and are not defined inside the class definition.

As a result, you have a compilation error.

To fix this error you need to translate operator+ to a free function: remove it's declaration from your Vector class and change it's sigature to (the easiest form):

Vector operator+ (const Vector& lhs, const Vector& rhs);

There are certain reasons to use a different signature, and accept first argument by value, but this is way too deep for your level, so I suggest to stick to const reference for now.

Thanks for the other inputs, Both have merit to them. I have found my own answer based off of The answer given by Barry @BarryTheHatchet. And other sources. To solve this problem I moved the definition within the header file in the public section, giving me:

 Vector & operator+=(const Vector & v){
    x += v.x;
    y += v.y;
    z += v.z;
    return *this;
}

Vector operator+(const Vector v) const {
    Vector VecPlus(*this);
    VecPlus.x += v.x;
    VecPlus.y += v.y;
    VecPlus.z += v.z;
    return VecPlus;
}

This was the result, I do hope it is a legitimate way to code it and not a shody way to do it but would lead to later problems. I've advanced in my project and it has raised none so far.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM