Ofek .T.
Ofek .T.

Reputation: 772

Template unable to match function definition to an existing declaration

I'm working on template class to Array but seems like the function doesn't match the declaration and I can't understand why:

template <class Type>
class Array
{
private:
    Type* m_data;
    unsigned int _size;

public:
    template <class Type> Array(unsigned int initial_size); // Function
}

As the function itself match the declaration, I even did Ctrl+'.' to let VS do the function but still getting the error. I saw similar porblems but seems like they all used Type in the param which I didn't.

template<class Type>
Array<Type>::Array(unsigned int initial_size)
{
    _size = initial_size;
    m_data = new Type(initial_size);
}

'Array::Array': unable to match function definition to an existing declaration

Why does the error happening?

Upvotes: 1

Views: 432

Answers (2)

R Sahu
R Sahu

Reputation: 206727

There is no need to use template in the constructor declaration:

template <class Type> Array(unsigned int initial_size);

Just use:

Array(unsigned int initial_size);

Upvotes: 2

eerorika
eerorika

Reputation: 238461

That's because there is no function (constructor) Array(unsigned int initial_size) declared in Array<T>. There is a constructor template: template <class Type> Array(unsigned int initial_size).

So, you have a constructor template inside a class template.

You could define it like this:

template<typename Type>
template<typename AnotherType>
Array<Type>::Array(unsigned int initial_size){

But since you don't appear to use the template arguement of the constructor template, it's unclear, why it is a template in first place.

If you don't need a constructor template, then I recommend using a regular constructor instead:

public:
Array(unsigned int initial_size);

Upvotes: 2

Related Questions