0

I created a class, ProcessQueue:

//ProcessQueue.h
template<class T>
class ProcessQueue{
public:
    void addTask(T param);
    void clearQueue();
    void addSetup(void (*setup)());
    void addProcess(void (*process)(T));
    void addCleanup(void (*cleanup)());
    void setup();
    void finish();
private:
    //Some private fields and functions that aren't relevant
}

addSetup and addCleanup add functions that are performed when setup and finish are called (The function pointers are then stored in a vector, but that shouldn't matter). addProcess should accept a void function that inputs a variable of type T, and addTask adds a variable of type T to a processing queue.

When clearQueue is called, the class iterates over each T that has been added and calls the various "process" functions that have been added to the ProcessQueue.

In another file (kernel.cu), I use a ProcessQueue to process the results of a computation:

(Note that this is part of the host code of the cu file, so it is regular C++. runRecord is simply a c-style struct)

//kernel.cu
ProcessQueue<runRecord> queue;

#ifdef PRINTTOFILE
queue.addSetup(setupPrintToFile);
queue.addProcess(processPrintToFile);
queue.addCleanup(cleanupPrintToFile);
#endif

#ifdef PRINTRESULTS
queue.addProcess(printRecord);
#endif

queue.setup();

//Do some computation
//Add the results to the ProcessQueue

queue.clearQueue();
queue.finish();

Some other function declarations that may be relevant:

//Various .h files
void setupPrintToFile();
void processPrintToFile(runRecord r);
void cleanupPrintToFile();
void printRecord(runRecord rec);

When I compile this, I get a few error messages:

1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::addTask(struct runRecord)" (?addTask@?$ProcessQueue@UrunRecord@@@@QAEXUrunRecord@@@Z)
1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::clearQueue(void)" (?clearQueue@?$ProcessQueue@UrunRecord@@@@QAEXXZ)
1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::addSetup(void (__cdecl*)(void))" (?addSetup@?$ProcessQueue@UrunRecord@@@@QAEXP6AXXZ@Z)
1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::addProcess(void (__cdecl*)(struct runRecord))" (?addProcess@?$ProcessQueue@UrunRecord@@@@QAEXP6AXUrunRecord@@@Z@Z)
1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::addCleanup(void (__cdecl*)(void))" (?addCleanup@?$ProcessQueue@UrunRecord@@@@QAEXP6AXXZ@Z)
1>kernel.cu.obj : error LNK2001: unresolved external symbol "public: void __thiscall ProcessQueue<struct runRecord>::finish(void)" (?finish@?$ProcessQueue@UrunRecord@@@@QAEXXZ)
fatal error LNK1120: 6 unresolved externals

I'm not too sure where the problem is. I've tried changing the template and function declarations, but it doesn't seem to help.

The compiler is nvcc v7.5.

Daniel M.
  • 1,433
  • 13
  • 24
  • Is you class split between a header and cpp file? – NathanOliver Aug 04 '16 at 15:42
  • Yes, they are all split. I'll add the locations of all of them in an edit – Daniel M. Aug 04 '16 at 15:43
  • You do *link* with the object file generated from the source file where all those functions are implemented? You *do* have an object file? You *do* have a source file? And it's actually in the *project* (and not only on the disk)? And the source file *do* have the definitions (implementations) of those functions? – Some programmer dude Aug 04 '16 at 15:44
  • You're showing where all of your functions are declared, in a header file. But you're not showing their definitions. You need to implement the functions, not just declare them. – Dan Korn Aug 04 '16 at 15:44
  • Yes, they are implemented – Daniel M. Aug 04 '16 at 15:45
  • So I need to put the implementation of `ProcessQueue` in the header file? (Or include the .cpp at the end of the header?) – Daniel M. Aug 04 '16 at 15:46
  • Templates can only be in header files. – NathanOliver Aug 04 '16 at 15:47
  • Nvcc isn't a compiler, and it isn't compiling any of the code in this question. What version of visual studio address you using? – talonmies Aug 04 '16 at 15:48
  • Writing `#include "ProcessQueue.cpp"` at the end of `ProcessQueue.h` solved the problem. – Daniel M. Aug 04 '16 at 15:49

0 Answers0