PDA

View Full Version : [C++] LNK2019 con template class


tuccio`
18-03-2011, 23:13
so già che sto facendo una domanda niubba :asd:

avrei necessità di implementare una coda di messaggi da usare con i thread con un modello produttore/consumatore (con un po' di consumatori)


#ifndef _SXLMESSAGEQUEUE_H_
#define _SXLMESSAGEQUEUE_H_

#include "Mutex.h"
#include "Semaphore.h"

#include <deque>

namespace sxl
{

namespace mt
{

/*
* Message queue per la comunicazione tra
* thread
*/

template <typename T>
class MessageQueue
{

private:

Semaphore sem;
Mutex m;
std::deque<T> queue;

public:

MessageQueue(void);

T Peek(void);
void Post(T& x);

};

}
}

#endif

#include "MessageQueue.h"

using namespace std;
using namespace sxl::mt;

template <typename T>
MessageQueue<T>::MessageQueue(void)
{
sem(0);
}

template <typename T>
T MessageQueue<T>::Peek(void)
{

// Si blocca in attesa che ci sia un
// messaggio disponibile
sem.Wait();

// Mutex sulla queue
m.Lock();

// Prende l'elemento che è da più tempo
// nella coda
T x = queue.front();
queue.pop_front();

m.Unlock();

return x;

}

template <typename T>
void MessageQueue<T>::Post(T &x)
{

// Mutex sulla queue
m.Lock();

queue.push_back(x);

m.Unlock();

// Segnala che c'è un elemento nella coda
sem.Signal();

}

Ho scritto questa classe generica che fa parte di una libreria statica che compila correttamente, ma evidentemente c'è un problema di linking, perché quando vado a compilare il progetto in cui uso questa classe ho questo output su visual studio:


1>ServerThread.obj : error LNK2019: riferimento al simbolo esterno "public: struct ServerThread::Request __thiscall sxl::mt::MessageQueue<struct ServerThread::Request>::Peek(void)" (?Peek@?$MessageQueue@URequest@ServerThread@@@mt@sxl@@QAE?AURequest@ServerThread@@XZ) non risolto nella funzione "private: static int __cdecl ServerThread::ServerProc(void *)" (?ServerProc@ServerThread@@CAHPAX@Z)
1>ServerThread.obj : error LNK2019: riferimento al simbolo esterno "public: __thiscall sxl::mt::MessageQueue<struct ServerThread::Request>::MessageQueue<struct ServerThread::Request>(void)" (??0?$MessageQueue@URequest@ServerThread@@@mt@sxl@@QAE@XZ) non risolto nella funzione "void __cdecl `dynamic initializer for 'public: static class sxl::mt::MessageQueue<struct ServerThread::Request> ServerThread::mq''(void)" (??__E?mq@ServerThread@@2V?$MessageQueue@URequest@ServerThread@@@mt@sxl@@A@@YAXXZ)


immagino di aver fatto qualche casino con i template, quale? non riesco a capire :<

tomminno
18-03-2011, 23:47
Metti tutto il codice della classe template nell'header.

tuccio`
18-03-2011, 23:59
così va in effetti, ma non c'è un altro modo? sembra un po' una porcata

Duchamp
19-03-2011, 00:04
così va in effetti, ma non c'è un altro modo? sembra un po' una porcata

All'inizio anch'io ero molto infastidito dalla cosa, ma poi ho dovuto rassegnarmi:


Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.


tratto da http://www.cplusplus.com/doc/tutorial/templates/