I have a class A in which most operations are implemented. Also, I have another class B which contains only a member of A. And I wish operations in A can be directly applied to B. So I define a conversion operation. But the compiler complains "error: no matching function for call to 'foo'". What's wrong with implicit conversion and how to implement this? Thanks.
Edit: What if I add an operator overloading to A and want B to use it directly?
template <typename T> struct B;
template <typename T>
struct A {
A(const B<T>& b) {} // Conversion from B to A, way 1
friend void foo(const A&);
// Addition
friend A operator+(const A&, const A&);
};
template <typename T>
void foo(A<T>& a) {}
// Addition
template <typename T>
A<T> operator+(const A<T>& a1, const A<T>& a2) { return A<T>(); }
template <typename T>
struct B {
B() {}
A<T> a;
operator A<T>() { return a; } // Conversion from B to A, way 2
// Addition
B(const A<T>& a) : a(a) {}
};
int main()
{
B<int> b;
foo(b);
auto bb = b+b;
}
foois a template. You call it in a way that requires argument deduction. There can be no conversion.