P45 Imminent
P45 Imminent

Reputation: 8591

Cannot match function declaration for template function in template class

Consider

template<class Y> struct foo
{
    template <class ForwardIt>
    foo(ForwardIt first, ForwardIt last);
};

In order to implement the constructor, I've written

template<class Y, class ForwardIt> foo(ForwardIt first, ForwardIt last)
{
    // ToDo - code here
}

But this generates a compile error to the effect that it cannot match that definition with a declaration.

What am I doing wrong? I'm using a C++11 compiler.

Upvotes: 1

Views: 105

Answers (2)

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

There are two issues in your code. First, you're missing the name of the class in the function definition outside of the class's body, which basically means you're declaring a freestanding function that has nothing to do with the class or its member function (in this case it is illegal because your function does not have a return type hence cannot be a freestanding function).

Secondly, you must use distinct template declarations for your class template parameters and member function template parameters.

You need:

template<class Y>
template<class ForwardIt>
foo<Y>::foo(ForwardIt first, ForwardIt last)
{
    // ToDo - code here
}

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234715

template<class Y>
template<class ForwardIt>
foo<Y>::foo(ForwardIt first, ForwardIt last)
{
}

is the correct way to define this.

Upvotes: 1

Related Questions