环境:clion mac
TmpOperator.h
#ifndef TEXT_TMPOPERATOR_H
#define TEXT_TMPOPERATOR_H
template <typename T>
class TmpOperator {
public:
T& test1(T& t);
};
#endif //TEXT_TMPOPERATOR_H
TmpOperator.cpp
#include "TmpOperator.h"
template<typename T> T& TmpOperator<T>::test1(T& t) {
return t;
}
main.cpp
#include <iostream>
using namespace std;
#include "TmpOperator.h"
int main() {
TmpOperator<int> tmpOperator;
int a = 1;
int b = tmpOperator.test1(a);
cout << b << endl;
}
看着代码没啥毛病啊,为啥报错啊
Scanning dependencies of target Text
[ 33%] Building CXX object CMakeFiles/Text.dir/main.cpp.o
[ 33%] Building CXX object CMakeFiles/Text.dir/TmpOperator.cpp.o
[ 50%] Linking CXX executable Text
Undefined symbols for architecture x86_64:
"TmpOperator<int>::test1(int&)", 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]: *** [Text] Error 1
make[2]: *** [CMakeFiles/Text.dir/all] Error 2
make[1]: *** [CMakeFiles/Text.dir/rule] Error 2
make: *** [Text] Error 2