0

I have a class with two friend operator overloadings inside:

template<typename T>
class A {
    T i;

    template <typename L, typename R>
    friend auto operator+(const A<L> &l, const A<R> &r) -> A<decltype(l.i + r.i)>;

    template <typename L>
    friend auto operator+(const A<L> &l, const T &r) -> A<decltype(l.i + r)>;
}

I wanted to know how can I create a definition for these functions in a .cpp file (since the declarations are in a header file).

For now I did this but it throws an error:

template <typename L, typename R>
auto operator+(const A<L> &l, const A<R> &r) -> A<decltype(l.i + r.i)>
{
    return Vector3Generic<decltype(l.i + r.i)>(l.x + r.x, l.y + r.y, l.z + r.z);
}

The error:

Undefined symbols for architecture x86_64:
  "A<decltype((fp.x) + (fp0.x))> operator+<float, float>(A<float> const&, A<float> const&)"

And I absolutely don't know how to define the second operator overload using the parent's T.

Nathanael Demacon
  • 1,169
  • 7
  • 19
  • Seems like I'm doomed to define these in the header file :( – Nathanael Demacon Jan 15 '20 at 10:40
  • 1
    @NathanaelDemacon Not necessarily: You *can* provide the implementation in a separate file. But you have to `#include` that one then in the header, usually at the very end (before closing header guard). Many use `.inl` as file ending in this case. And of course, you have to provide that `.inl` file together with your header. Not necessarily in same directory, you'd then find it via appropriately adjusted include paths (via `-I` (capital i) flag on most compilers). – Aconcagua Jan 15 '20 at 10:44
  • @Aconcagua this is exactly what I wanted! I only wanted to have a "cleaner" way of doing the implementation by separating it in another file - `.inl` is the solution. Thanks you. – Nathanael Demacon Jan 15 '20 at 11:04
  • Sadly it was making too much of a repetition so I ended up defining it in the header file. – Nathanael Demacon Jan 15 '20 at 11:24

0 Answers0