Pyjong
Pyjong

Reputation: 3197

instantiated from here error

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

Upvotes: 3

Views: 7532

Answers (3)

Clifford
Clifford

Reputation: 93446

GCC can split error messages across multiple reports in order to describe problems that have multiple locations. You may need to read the the messages as one single message, and may even need the message that preceeds this one to make sense.

Also post logs in code mark-up to force it to appear verbatim.

Upvotes: 0

HappyCodeMonkey
HappyCodeMonkey

Reputation: 436

I got this to build on my own system, I could be wrong, but I think your problem is that there isn't a default constructor for sPacket:

class sPacket {

public:

sPacket() { } //empty default constructor

sPacket(string s, int a) {s=s; a=a;}

};

I hope this is helpful!

Upvotes: 0

anon
anon

Reputation:

As it stands, your code needs a default constructor for the type T. Change your template constructor to:

 tListItem(T t)  : tData(t), next(0) {}

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.

Upvotes: 3

Related Questions