c++ - 未定义对完整模板特化类成员函数的引用,但不是部分特化
问题描述
所以当使用具有完整模板类专业化的模板显式实例化时,我得到了一个未定义的引用错误,但问题是,部分模板类专业化运行良好,没有错误。
代码如下所示,有人知道为什么吗?在这种情况下,完全专业化和部分专业化有什么区别?
提前致谢。
// t.h
#include <iostream>
using namespace std;
template <typename T1, typename T2>
class A {
public:
void foo();
};
// t2.cpp
#include "t.h"
template<typename T1>
class A<T1, int> {
public:
void foo() {
cout << "T1, int" << endl;
}
};
template<>
class A<int, int> {
public:
void foo() {
cout << "int, int" << endl;
}
};
template class A<float, int>;
template class A<int, int>;
// t.cpp
#include "t.h"
int main() {
A<float, int> a;
a.foo(); // no error
A<int, int> a1;
a1.foo(); // undefined reference error, why?
return 0;
}
编译命令g++ t.cpp t2.cpp -o t
使用 gcc 4.8.5。
解决方案
推荐阅读
- sql - 带有“Union”和“GroupBy”的“Order By”(Oracle)
- javascript - JavaScript 应用函数的第一个参数
- java - Spring Boot Oauth2 从错误的数据源加载客户端详细信息
- python - 带有 for 循环和函数调用的函数
- sql-server - SQL 创建和填充
- terraform - Terraform 应用时触发循环错误
- python - 检查文件夹图像的扩展类型并确保所有图像都是一种格式
- java - 配置向导启动和关闭
- swift - 将方法乘以 Swift 中的属性
- android - 使用firebase Flutter登录时的进度条