Error created here

my compiler tormented me by this creation error, which I do not fully understand.

I have a listItem class class:

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 its object with a non-primitive data type, for example, for example:

sPacket zomg("whaever",1);
tListItem<sPacket> z(zomg);

my compiler always throws this error .. the error is not generated by primitive types.

compiler output:

../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 would not bother you, but I do not want to spend 2 hours on something stupid ..... so for all your answers

+3
source share
3 answers

In a way, your code needs a default constructor for type T. Change the template constructor to:

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

, T . , .

+3

, , , , , sPacket :

class sPacket {

:

sPacket() {}//

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

};

, !

0

GCC , . , , , , .

Also publish magazines in code markup to make it appear verbatim.

0
source

Source: https://habr.com/ru/post/1724876/


All Articles