1

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.

2 Answers 2

5

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
}
Sign up to request clarification or add additional context in comments.

Comments

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

is the correct way to define this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.