Hey, piszę w tym dziale bo chyba jest to bardzo zaawansowane C++ a w Newbie chyba ludzie nie ogarniają o co mi chodzi :) (faktycznie trochę też zagmatwałem). Chodzi o to że z jakiegoś dziwnego powodu w projekcie QT nie kompilator chce mi utworzyć obiektu klasy szablonowej a na czystym projekcie w Visualu to działa. Co jest nie tak z tym kodem?
.hpp
//naglowki,
#include "Ping.h"
template <class T> class Message
{
public:
Message(const quint8 & type);
~Message();
void receiver(qint16 receiverId);
void content(T * content);
quint8 type() const;
quint16 length() const;
quint16 receiver() const;
T * content() const;
private:
quint8 _type;
quint16 _length;
quint16 _receiver;
T * _content;
};
#endif // MESSAGES_HPP
.cpp
template <class T> Message<T>::Message(const quint8 & type) : _receiver(0), _type(type)
{
_length = sizeof(T);
}
template <class T> void Message<T>::receiver(qint16 receiverId) {
_receiver = receiverId;
}
template <class T> void Message<T>::content(T *content) {
_content = content;
}
template <class T> quint16 Message<T>::length() const {
return _length;
}
template <class T> quint8 Message<T>::type() const {
return _type;
}
template <class T> quint16 Message<T>::receiver() const {
return _receiver;
}
template <class T> T * Message<T>::content() const {
return _content;
}
template <class T> Message<T>::~Message() {
//delete _content;
}
Przy próbie utworzenia obiektu (w metodzie klasy Client):
Message <Ping> message(0);
wywala błąd:
Client.obj:-1: błąd: LNK2019: unresolved external symbol "public: __cdecl Message<class Ping>::Message<class Ping>(unsigned char const &)" (??0?$Message@VPing@@@@QEAA@AEBE@Z) referenced in function "public: void __cdecl Client::sendHelloMessage(class QString const &)" (?sendHelloMessage@Client@@QEAAXAEBVQString@@@Z)
Client.obj:-1: błąd: LNK2019: unresolved external symbol "public: __cdecl Message<class Ping>::~Message<class Ping>(void)" (??1?$Message@VPing@@@@QEAA@XZ) referenced in function "public: void __cdecl Client::sendHelloMessage(class QString const &)" (?sendHelloMessage@Client@@QEAAXAEBVQString@@@Z)
Chcę ten utworzony obiekt przesłać jako parametr innej funkcji send (wysyłanie wiadomości)
Stworzyłem bardzo podobny kod (który nic nie robi ale jest bardzo podobny do powyższego) w czystym projekcie w visualu i działa normalnie.