Home > Back-end >  Can't create constructor for template type with template type parameter
Can't create constructor for template type with template type parameter

Time:09-17

Disclaimer: I'm very new to C .

I have a Container class and an Item class. Both of them are generic with the same Type:

container.h:

template<typename Type>
class Container
{
public:
    Container(Item<Type> item);
}

container.cpp:

#include "container.h"
#include "item.h"

template<typename Type>
Container<Type>::Container(Item<Type> item)
{
}

The problem is that an error is showing on the Container's constructor:

no instance of overloaded function "Container::Container" matches the specified type

constructor error

How can I fix this?

CodePudding user response:

You need to create container.tpp (template file) and write implementation of your class method in container.tpp

Like this:

container.h:

template<typename Type>
class Container
{
public:
    Container(Item<Type> item);
}
#include" container.tpp" //write this at the end of container.h

In container.tpp

container.tpp:

template<typename Type>
Container<Type>::Container(Item<Type> item)
{
}

CodePudding user response:

Usually it is done like this, container.h

template<typename Type>
class Container
{
public:
    Container(const Item<Type>& item)
    {
       // implementation goes here
    }
};
  • Related