views:

241

answers:

3

hi,

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::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

+2  A: 

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.

anon
what's the difference?
stupid_idiot
well, it works, throws still some warnings though, but i'm still not sure how come it doesn't work the same.. my version should use the default copy constructor too.
stupid_idiot
If you class is instantiated all default ctors of its members will be called if you don't intialize them in your constructor list. If one of your member types isn't default constructible this will fail.
pmr
No it shouldn't. It will use the default assignment operator and WILL require adefault constructor.
anon
oh, yes! now i see.. thx so much, you have spared me a lot of time trying to solve this
stupid_idiot
@stupid_idiot: if you had used the normal practice me preferring member initialization list over the assignment you wouldn't have got the error .
Naveen
what what what?! you mean like neil b. announced?
stupid_idiot
A: 

Hi there, 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!

HappyCodeMonkey
Providing an uneccessary default constructor is always a bad idea. Coincidentally, I just blogged about this at http://punchlet.wordpress.com/
anon
I didn't realize, thanks for the comment! Your post is very interesting.
HappyCodeMonkey
A: 

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.

Clifford