Szablon klasy nie tworzy się.

0

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.

2

Przenies definicje metod do pliku naglowkowego.

0

Dzięki. Skompilowało się. Natomiast jest problem z drugą częścią czyli przekazaniem tego obiektu przez parametr.

class Client {
 //rozne rzeczy
   template <class T> void Client::send(Message<T> & message) {
  

   }
}

i chcę przekazać do niej tę wiadomość:

Client client;
Message <Ping> message(0); //ok teraz kompiluje

client.send(message); //blad taki sam w sumie jak te poprzednie tylko nie 2 a 1

Btw. Trochę dziwne że wszystko w .hpp jest a nic w .cpp

2
#include <iostream>
using namespace std;

template<typename T>
class Message { };

class Client {
public:
	template<typename T>
	void Send(Message<T> message) {
		cout << "Send";
	}
};

int main() {
	Client cli;
	cli.Send<int>(Message<int>());
	return 0;
}

http://ideone.com/CfjXDl

0

Hmm Twój kod jest chyba analogiczny do mojego

    Client client;
    client.send<Ping>(Message<Ping>(1));

Jednak pojawia się mimo wszystko błąd:

main.obj:-1: błąd: LNK2019: unresolved external symbol "public: void __cdecl Client::send<class Ping>(class Message<class Ping> &)" (??$send@VPing@@@Client@@QEAAXAEAV?$Message@VPing@@@@@Z) referenced in function main

Zamieniłem "class" na "typename" jednak bez efektu.

0

Faktycznie, zapomniałem że w klasie Client też wszystko ma być w nagłówku (metoda send). Dzięki wielkie. Ostatnie pytanie: dlaczego właśnie tak? To mało estetyczne jest a pewnie dałoby się zrobić tak żeby nie psuć sobie stylistyki (rozdziału deklaracji od definicji).

1

W skrocie, tak jest najprosciej to zrobic, szerzej: http://www.parashift.com/c++-faq/templates-defn-vs-decl.html

0

Ok, dzięki. Niech jakiś moderator da plusa koledze bo coś nie mogę. Pzdr!

1 użytkowników online, w tym zalogowanych: 0, gości: 1