Question

I have created my own blocking queue and I'm having some trouble figuring out why I get a linker error (note this is a Qt app in 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

The implementation is a bit longer, so I have a pastie for it: http://pastie.org/5368660

The program entry point looks like this:

#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();
}

The linker error I get is:

Error   1   error LNK2019: unresolved external symbol "public: __thiscall TestingNS::BlockingQueue<class Item>::BlockingQueue<class Item>(void)" (??0?$BlockingQueue@VItem@@@TestingNS@@QAE@XZ) referenced in function _main

I don't understand why the linker can't find the constructor (nor any other method from BlockingQueue). Any ideas?

Was it helpful?

Solution

It's template, you have to put the implementation inside BlockingQueue.h

For a while, the standard did provide the keyword export to allow such a separate implementation file. But not many vendors implemented it. C++11 discontinues that use of export but reserves the export keyword for possible future use.)

Templates have to be used in conjunction with requests for particular instantiations of templates.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top