-1

I am working on a class for a c++ project, and I have to overload all of my operators to work on vectors. Specifically, I define my vector as follows:

template<class Type>
ThreeVector<Type>::ThreeVector(Type x, Type y, Type z) {
    mx=x;
    my=y;
    mz=z;
}

And my +operator as:

template<class Type>
ThreeVector<Type> operator+(const ThreeVector<Type>& v, const//
ThreeVector<Type>& w) {

    return ThreeVector<Type>(v)+=w;
}

Where I have already overloaded the += and -= operators. However, I keep getting this error:

ThreeVT.cxx:12:26: error: no matching function for call to// 
‘ThreeVector<double>::ThreeVector(ThreeVector<double>)’
ThreeVector<double> d=c+a;

ThreeVector.h:141:29: error: no matching function for call to 
‘ThreeVector<double>::ThreeVector(const ThreeVector<double>&)’
 return ThreeVector<Type>(v)+=w;

Any help would be much appreciated! This error seems to come up no matter what I do, and I don't know what it really means in this context.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Ross Bauer
  • 13
  • 1
  • 5
  • There's not much sense in taking the first argument by const reference if you're always going to copy it. Anyway, please include an [MCVE](http://stackoverflow.com/help/mcve) so everyone can easily get the error for themselves and test it. – chris Apr 28 '15 at 15:13
  • It looks like you might need to define a copy constructor for your `ThreeVector` class template. But it's hard to say without the full class definition, and the precise lines of code the compiler is complaining about. Please consider posting more of the code, or giving a link to a minimal, self-contained, live example on e.g. ideone.com or coliru.stacked-crooked.com or a similar live compiler. As a final, unrelated, note, you may as well accept the first argument to operator+ by value, since you immediately make a copy anyway. – Andrew Apr 28 '15 at 15:15
  • You' re using the operator + in its definition : `return ThreeVector(v)+=w;` I am not sure what are you doing here – Othman Benchekroun Apr 28 '15 at 15:15
  • Thats true. Originally, I didnt have it as const reference, but trying to debug with a classmate he suggested it.It did not change the error message – Ross Bauer Apr 28 '15 at 15:15
  • @Othman, It's defining `operator+` in terms of `operator+=`, which is normal (and good). The method of implementing it as such is not [what you'd normally see](http://stackoverflow.com/a/4421719/962089), however. – chris Apr 28 '15 at 15:16
  • Sorry I didn' t see that you overloaded the operator +=, can you put the code ? – Othman Benchekroun Apr 28 '15 at 15:17
  • @Othman, I overloaded the += operator first, as the professor suggested that it was needed to overload the + operator – Ross Bauer Apr 28 '15 at 15:18
  • Here is the code, thanks @Andrew for the tip using ideone – Ross Bauer Apr 28 '15 at 15:19
  • https://ideone.com/1KyMjt – Ross Bauer Apr 28 '15 at 15:19
  • for further clarification, my int main() simply asks to add two vectors, and then print out the result. Nothing else – Ross Bauer Apr 28 '15 at 15:22
  • 1
    @RossBauer Your `operator+=` should be returning a reference, not a brand new object. Same thing with `operator-=`. – PaulMcKenzie Apr 28 '15 at 16:04

2 Answers2

0

You have several problems:

Functions that take a reference:

ThreeVector( ThreeVector&);
ThreeVector<Type> operator=( ThreeVector&);
ThreeVector<Type> operator+=( ThreeVector&);
ThreeVector<Type> operator-=( ThreeVector&);
ThreeVector<Type> operator-( ThreeVector&);

Should take a const reference, unless they actually change the parameter:

ThreeVector(const ThreeVector&);
ThreeVector<Type> operator=(const ThreeVector&);
ThreeVector<Type> operator+=(const ThreeVector&);
ThreeVector<Type> operator-=(const ThreeVector&);
ThreeVector<Type> operator-(const ThreeVector&);

Your accessor functions are non-const:

Type x();
Type y();
Type z();

But should be const:

Type x() const;
Type y() const;
Type z() const;

All of these changes should be made both in the class body and the function definitions.

Now, on to your operator+: operator+ can either be a free function or a member function. Either way, you need a left-hand side and a right-hand side. If operator+ is a member function than the lhs is always this. Thus, for a binary + function your declaration looks like this:

ThreeVector<Type> operator+(const ThreeVector& rhs) const;

Note that the parameter is passed by const ref, and the function itself is const, so it can be called on a const ThreeVector.

The implementation in your code was missing the class name. It should look like:

template<class Type>
ThreeVector<Type> ThreeVector<Type>::operator+(const ThreeVector<Type>& rhs) const

Then, the body of your function can make use of the this keyword:

return ThreeVector<Type>(*this) += rhs;

A note about operator+=: The standard convention is that operator+=, -=, etc. return a reference to the object that just changed. return *this. Your function should look like:

ThreeVector<Type>& operator+=(const ThreeVector&);
Bill
  • 14,257
  • 4
  • 43
  • 55
  • Thank you for the input. It has helped with my initial error. However, I still need help with the + operator. I am receiving an error I had received previously: It tells me that my operator overload line must take 0 or 1 argument. But this does not make sense to me. By definition, adding two of anything requires two arguments. Am I interpreting this incorrectly? – Ross Bauer Apr 28 '15 at 15:51
  • All class functions take an implicit first parameter of `this`. A class function that adds already has the left-hand side... it's `this`! So you're either writing a unary +, which needs no other argument, or binary + which needs one other argument. – Bill Apr 28 '15 at 18:49
  • @Quentin: Thanks, for some reason I thought they were necessary, but the problem I was working on was fixed by adding `const`. – Bill Apr 28 '15 at 18:50
  • @RossBauer: I added more information about operator+. – Bill Apr 28 '15 at 19:08
0

The left argument in the + operator is the object itself and found by the "this" pointer. It's members are found as in any other member function. So your + operator chould be declared as

const ThreeVector<Type> operator+(const ThreeVector<Type>& v) const;

The last const means that the left object is not altered In 5 + 2 either 5 or 2 is altered to create 7