2012-11-12 143 views
0

我已经创建了自己的阻塞队列,我遇到了一些麻烦搞清楚为什么我得到一个链接错误(注意这是一个Qt应用程序在Visual Studio 2010):解析外部符号

#ifndef BLOCKING_QUEUE_H 
#define BLOCKING_QUEUE_H 

#include <QObject> 
#include <QSharedPointer> 
#include <QWaitCondition> 
#include <QMutex> 
#include <queue> 

namespace TestingNS 
{ 
    template<typename Data> 
    class BlockingQueue 
    { 
    private: 
     std::queue<QSharedPointer<Data>> _queue; 
     QMutex _mutex; 
     QWaitCondition _monitor; 
     volatile bool _closed; 

    public: 
     BlockingQueue(); 

     void Close(); 

     size_t Size(); 

     void Empty(); 

     bool IsClosed(); 

     bool Enqueue(QSharedPointer<Data> data); 

     bool TryDequeue(QSharedPointer<Data>& value, unsigned long time = ULONG_MAX); 
    }; 
} 
#endif //BLOCKING_QUEUE_H 

的实现是多一点的时间,所以我有一个pastie它:http://pastie.org/5368660

程序入口点是这样的:

#include <QtCore/QCoreApplication> 
#include <QTimer> 
#include <iostream> 
#include "BlockingQueue.h" 

using namespace std; 
using namespace TestingNS; 

class Item 
{ 

}; 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 

    BlockingQueue<Item> queue; 

    cout << "Press any key to exit!" << endl; 

    char in; 
    cin.get(in); 
    QTimer::singleShot(0, &a, SLOT(quit())); 

    return a.exec(); 
} 

链接器错误我得到的是:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall TestingNS::BlockingQueue<class Item>::BlockingQueue<class Item>(void)" ([email protected]@@@[email protected]@[email protected]) referenced in function _main 

我不明白为什么链接器无法找到构造函数(也不是来自BlockingQueue任何其他方法)。有任何想法吗?

+0

BlockingQueue的实现在哪里? – imreal

+2

看起来像另一个受骗者为[为什么模板只能在头文件中实现?(http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-文件)。 –

+0

@Nick在我的问题中看到了pastie链接。 – Kiril

回答

3

这是template,你必须把实现内部BlockingQueue.h

有一段时间,标准的确 提供关键字export允许这样一个单独的实现文件。但并不是很多供应商实施了它。 C++ 11中止在使用出口的,但保留为将来可能使用的export 关键字。)

模板具有在结合使用与特定的模板 实例请求。