我的编译器用这个我完全不理解的实例化错误折磨着我。
我有模板类 listItem:
template <class T>
class tListItem{
public:
tListItem(T t){tData=t; next=0;}
tListItem *next;
T data(){return tData;}
private:
T tData;
};
如果我尝试使用非原始数据类型初始化它的对象,例如:
sPacket zomg("whaever",1);
tListItem<sPacket> z(zomg);
我的编译器总是抛出这个错误..这个错误不是用原始类型抛出的。
编译器的输出是:
../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&)
我不会打扰你,但我不想花 2 个小时在一些愚蠢的事情上......所以感谢你的所有回复
最佳答案
就目前而言,您的代码需要类型 T 的默认构造函数。将您的模板构造函数更改为:
tListItem(T t) : tData(t), next(0) {}
不同之处在于您的版本默认构造类型 T 的实例,然后分配给它。我的版本使用初始化列表来复制构造实例,因此不需要默认构造函数。
关于c++ - 从这里实例化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1855948/