My template class compile successful but link error.
Error message:
Undefined symbols for architecture x86_64:
"calculate<int>::plus(int, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Code is very simple:
1. calcualte.hpp
template <class T>
class calculate {
public:
T plus(T a, T b);
};
2. calculate.cpp
template <class T>
T calculate<T>::plus(T a, T b) {
return a+b;
}
3. main.cpp
calculate<int> cal;
int a = cal.plus(11,20);
If I move the code in calculate.cpp to calculate.hpp, it will build successful. It should be link configuration in XCode. Anyone can help?