Wednesday, 24 January 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

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...