wocan23
2019-09-24 15:54
采纳率: 100%
浏览 537

c++简单类模板报错问题

环境: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

2条回答 默认 最新

相关推荐 更多相似问题