my compiler is torturing me with this instantiation error which I completely don’t understand.
i have template class listItem:
template <class T>
class tListItem{
public:
tListItem(T t){tData=t; next=0;}
tListItem *next;
T data(){return tData;}
private:
T tData;
};
if i try to initialize an object of it with non-primitive data type like e.g:
sPacket zomg("whaever",1);
tListItem<sPacket> z(zomg);
my compiler always throws this error.. the error isnť thrown with primitive types.
output from compiler is:
../linkedList/tListItem.h: In constructor ‘tListItem<T>::tListItem(T) [with T = sPacket]’:
recvBufTest.cpp:15: instantiated from here
../linkedList/tListItem.h:4: error: no matching function for call to ‘sPacket::sPacket()’
../packetz/sPacket.h:2: note: candidates are: sPacket::sPacket(const char*, int)
../packetz/sPacket.h:1: note: sPacket::sPacket(const sPacket&)
i wouldn’t bother you but i don’t want to spend 2 hours with something stupid….. so thx for all your replies
As it stands, your code needs a default constructor for the type T. Change your template constructor to:
The difference being that your version default constructs an instance of type T and then assigns to it. My version uses an initialisation list to copy construct the instance, so no default constructor is required.