mardi 29 mars 2016

c++; ld: symbol(s) not found for architecture x86_64, generic class [duplicate]

This question already has an answer here:

Shortly: I have problem Undefined symbols for architecture x86_64.

In details: I've build simple project to show my problem.

//test.h
template <class T>
class Test {
    public:
    Test(T t);
};

//test.cpp
#include "test.h"

template <class T>
Test<T>::Test(T t) {}

//test_main.cpp
#include "test.h"

int main(int argc, char **argv) {
    int param = 0;
    Test<int> test(param);
}

After all - I have this error (compile with -std=c++11):

Undefined symbols for architecture x86_64:

"Test<int>::Test(int)", referenced from:

_main in test_main.o

ld: symbol(s) not found for architecture x86_64

Also it's clear if I delete template and change everything to int - I'm fine. But I would really like to understand what is the correct way to call function from a class with a template.

I know that there are some questions like this already around but actual reason why this problem happens - I didn't find. So please help me to understand! Some link to documentation with reason why what I'm writing is wrong would be very welcome!

Thank you!

Aucun commentaire:

Enregistrer un commentaire