I was trying to make overload operator+ in templated class "test", it works with the same types and with converter I was able to add class object and regular type(e.g. test + int). But, when I try to add two class objects with different types I get this message
error C2666: 'test::operator +': 2 overloads have similar conversions ...Templater.cpp(14,17): message : could be 'test test::operator +(test,test)' [found using argument-dependent lookup] ...Templater.cpp(14,17): message : or 'test test::operator +(test,test)' [found using argument-dependent lookup] ...Templater.cpp(25,18): message : while trying to match the argument list '(test, test)'
Here's the code:
#include <iostream>
using namespace std;
template <class T>
class test {
public:
T first, second;
test(T a = 0, T b = 0) : first(a), second(b) {};
//converter
template <typename X>
operator test<X>() {
return test<X>(first, second);
}
friend test operator+(test left, test right) {
return test(left.first + right.first, left.second + right.second);
}
friend std::ostream& operator<<(std::ostream& Str, test c) {
return (Str << c.first << " " << c.second);
}
};
int main() {
test<float> a(1.2, 5.4);
test<int> b(4.7, 17.5);
cout << a + b;
}
I was looking for an answer, but only found cases with same types