Alright, I’m trying to figure out this error and have, so far, had absolutely no luck. I’m doing this for homework, which is why I’m not using included classes.
Here’s the relevant code:
//Will return an array where each element is the sum of the respective sums in the added arrays
Vec& Vec::operator+(Vec& v2) const{
Vec ret();
if(mySize>0){
Vec ret(mySize);
for(unsigned i = 0;i<mySize;i++){
ret[i]=v2[i]+myArray[i];
}
}
else{
}
return ret;
}
And from the .h file…:
Vec& operator+ (Vec& v2) const;
This throws the error: “invalid initialization of non-const reference of type ‘Vec&’ from an rvalue of type ‘Vec (*)()’”
I’m completely new to C++, so any help would be appreciated.
Is taken to be a forward declaration of a function which takes no arguments and returns a
Vec. See: the most vexing parse.Next, you’re returning a reference to a local variable, which is bad.
retgoes out of scope as soon as the function returns.