dimanche 13 mai 2018

Undefined symbols for architecture x86_64 when using Templates with C++11

I got a homework to implement a Priority Queue in C++ with CLion and change the parameter for the priority from float to a generic Type using templates.

After implementing I always get the following error:

Undefined symbols for architecture x86_64:
 "PriorityQueue<float>::PriorityQueue()", referenced from:
      _main in main.cpp.o
  "PriorityQueue<float>::~PriorityQueue()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [Template] Error 1
make[2]: *** [CMakeFiles/Template.dir/all] Error 2
make[1]: *** [CMakeFiles/Template.dir/rule] Error 2
make: *** [Template] Error 2

PriorityQueue.h

...
#include <iostream>
using namespace std;

template <typename T>
class PriorityQueue {
public:
    struct pqentry {
        string value;
        T priority;
        pqentry *next;
    };
public:
    PriorityQueue();
    ~PriorityQueue();
    void insert(const string &value, T priority);
    void print();
    void remove(string value);
    string extractMin();
    void decreaseKey(string value, T priority);
    bool isEmpty();

private:
    int size;
    pqentry *begin;
};
...

PriorityQueue.cpp

All functions look like these two for example:

template <typename T>
PriorityQueue<T>::PriorityQueue() {
    size = 0;
    begin = nullptr;
    cout << "Created list successfully! " << "Size: " << size << ", Begin: " << begin << endl;
}

template <typename T>
PriorityQueue<T>::~PriorityQueue() {
...
}

Main.cpp

In my main function I try to create an element using a float like this and then I get the error above.

#include <iostream>
#include "PriorityQueue.h"
#include "QueueExceptions.h"

int main() {

    PriorityQueue<float> p;
return 0;
}

I cannot figure out what the problem is. I use C++11 and CLion on MacOS (lates version).

Aucun commentaire:

Enregistrer un commentaire