Reputation:
I wrote the following code using C++11 standard:
.h file:
#include "Auxiliaries.h"
class IntMatrix {
private:
Dimensions dimensions;
int *data;
public:
int size() const;
IntMatrix& operator+=(int num);
};
Bit I am getting and error saying that:
error: use of overloaded operator '+' is ambiguous (with operand types 'const mtm::IntMatrix' and 'int') return matrix+scalar;
Any idea of what causes this behaviour and how may I fix it?
Upvotes: 2
Views: 10296
Reputation: 37606
You declared the operators in the mtm
namespace so the definitions should be in the mtm
namespace.
Since you define them outside, you've actually two different functions:
namespace mtm {
IntMatrix operator+(IntMatrix const&, int);
}
IntMatrix operator+(IntMatrix const&, int);
When you do matrix + scalar
in operator+(int, IntMatrix const&)
, both functions are found:
You need to define the operator
s in the namespace you declared them, mtm
:
// In your .cpp
namespace mtm {
IntMatrix operator+(IntMatrix const& matrix, int scalar) {
// ...
}
}
Upvotes: 6