C++报错找不到实现

如题,现在同一个目录下有三个文件。

// 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
报错:
image.png

阅读 2.2k
1 个回答

模板的实现必须在编译期可见。把 test.cpp 里的内容放 test.h 里面去吧 ...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏