Wednesday, January 24, 2018

c++ - I have three cpp file.I want test that template class declaration and definition in different file.but i got failed

this is my code snnippet




// test.cpp
#include
#include "test.h"
int main(){
Test test = Test();

test.add(1);
test.print();
}


// test.h
#ifndef TEST
#define TEST
template class Test{
public:
T info;
void print();
void add(T i);
};
#endif

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

template void Test::print()
{
std::cout << info << std::endl;
}
template void Test::add(T i)
{

info = i;
}


and i run this exec



g++ -c testT.cpp && g++ test.cpp -o a.out testT.o


i get this error




Undefined symbols for architecture x86_64:
"Test::add(int)", referenced from:
_main in test-e89092.o
"Test::print()", referenced from:
_main in test-e89092.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation) // in my macOS



i want to declare template class in head file and define method in other file,so i only compile the define method file not head file if i change the definition of method in template class. what should i do?

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...