Question

I have this hpp

namespace A
{
    template<class T>
    class MyC
    {
    public:
        T a;
    };

    template<class T>
    void F(T r);
}

and this cpp

template<>
void A::F<double>(double r)
{
    r;
}

template<>
void A::F<int>(int r)
{
    r;
}

template<class T>
void A::F<A::MyC<T>>(A::MyC<T> r)
{
    r;
}
template void A::F<A::MyC<int>>(A::MyC<int>);
template void A::F<A::MyC<double>>(A::MyC<double>);

but compiler says "unable to match function definition to an existing declaration" about F.

What's wrong with this code?

Was it helpful?

Solution

Put those declarations all in namespace A { ... } and remove A::. On the other hand, function template partial specialization is not allowed and this will make error:

template<class T>
void F<MyC<T>>(MyC<T> r)
{
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top