0

I have a struct template A<x> and B<x>, an operator overload for A<x>+int and a conversion from B<x> to A<x>. I want B<x> to be converted to A<x> when adding with int, so I used friend function declaration.

#include <iostream>
template<int x>
struct A{
    int a;  
};

template<int x>
struct B{
    int b=3;

    operator A<x>(){
        return {b+10};
    }
    friend int operator+(A<x> a, int b);

};


template<int x>
int operator+(A<x> a, int b){
    return a.a+b;
}


int main(){
    std::cout<<(A<12>{9}+10)<<std::endl; // This works fine
    std::cout<<(B<12>{9}+10)<<std::endl; // Error
    return 0;
}

Now the linker complains that 'operator+(A<12>, int)' is not defined. Could somebody tell me what's wrong with this?

eivour
  • 1,678
  • 12
  • 20

0 Answers0