如题,现在同一个目录下有三个文件。
// test.h
#ifndef FILE_TEST_H
#define FILE_TEST_H
template <typename T> class test {
public:
int tt();
};
#endif //FILE_TEST_H
// test.cpp
#include "test.h"
template <typename T>
int test<T>::tt() {
return 123;
}
//main.cpp
#include <stdio.h>
#include "test.h"
int main()
{
test<int> t = test<int>();
printf("%d", t.tt());
}
为什么运行g++ main.cpp test.cpp
报错:
模板的实现必须在编译期可见。把 test.cpp 里的内容放 test.h 里面去吧 ...