zhngfrank
zhngfrank

Reputation: 11

Having issues overloading "+,-,*" operators for vectors- "no match for operator..."

I'm attempting to overload the basic arithmetic operators for vectors. The idea is to create a calculator that adds, subtracts, and multiplies very long numbers (e.g. with a length of 4000). When I add two vectors, I want my program to loop from the beginning of two vectors, add the values, then push them to a third vector. I'm aware that I shouldn't overload std classes, or that I could accomplish this using stl and boost, but it isn't allowed for my project.

My main issue is that my program doesn't seem to recognize my overloaded operators. Here's a code snippet:

from

//calculator.h
class Calculator {
public: 

....

void Solve();

friend std::vector<int> operator+(const std::vector<int> &op1, const std::vector<int> &op2);
friend std::vector<int> operator-(const std::vector<int> &op1, const std::vector<int> &op2);
friend std::vector<int> operator*(const std::vector<int> &op1, const std::vector<int> &op2);

}

and

//calculator.cpp
void Calculator::Solve() {
    ...
    if(operation == '-') {
        op1 = op1 - op2;
    }
    else if(operation == '+') {
        op1 = op1 + op2;
    }
    else if(operation == '*') {
        op1 = op1 * op2;
    }
    ...
}

friend std::vector<int> operator+(const std::vector<int> &op1, const std::vector<int> &op2) {
    std::vector<int> toreturn;
    ...
    //use a couple loops to add vectors together
    ...
    return toreturn;
}

when I compile with g++ (-v = 5.4, ubuntu 16.04), I get the following error:

error: no match for ‘operator-’ (operand types are ‘std::vector<int>’ and ‘std::vector<int>’)
polynomial1 = polynomial1 - polynomial2;
                          ^
In file included from /usr/include/c++/5/vector:65:0,
             from Calculator.h:18:
/usr/include/c++/5/bits/stl_bvector.h:208:3: note: candidate: std::ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
operator-(const _Bit_iterator_base& __x, const _Bit_iterator_base& __y)
^
/usr/include/c++/5/bits/stl_bvector.h:208:3: note:   no known conversion for argument 1 from ‘std::vector<int>’ to ‘const std::_Bit_iterator_base&’
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
             from /usr/include/c++/5/vector:60,
             from Calculator.h:18

I've attempted to make the vector overloads on a global scope (they don't work with private members of the Calculator class), attempting to make them member functions (using a single argument, rhsObj, and this), setting op1=op1+op2 to std::vector<int> temp = op1 + op2. Nothing has worked so far. I've even attempted to make a public function std::vector<int> Add(std::vector<int> op1, std::vector<int> op2) with essentially the same code and it hasn't worked.

Any help would be greatly appreciated!

Upvotes: 1

Views: 123

Answers (1)

M.M
M.M

Reputation: 141628

Your code doesn't compile because friend declarations only make the name available to argument-dependent lookup.

Since the arguments are in std, then only std is searched for operators. Your function is not in std so it is not found.

Before you try adding your operator to std - don't, that's undefined behaviour.

It would be possible to provide non-friend declarations, so that the names are found as part of the step of regular lookup that searches enclosing scopes (see example - credit to BlackMoses).

However this is a bad idea in two ways:

  1. Lookup of these operators would be inconsistent; e.g. consider this modification of the above code - if the operator name is found in a narrower namespace it will never get up to the enclosing namespace. Because of this, you should only ever make operator overloads such that they are found by ADL (i.e. at least one operand is in the same class or namespace as the overloaded operator function).

  2. This is a poor use of overloaded operators anyway. What if someone else incorporating your code didn't want those overloads? And they do not help with the users of your class. The idea of operator overloading is to provide intuitive syntax for people using your class (Calculator in this case). You can just use normal functions to implement your class's member functions.

Upvotes: 4

Related Questions