模版与头文件

版权声明: https://blog.csdn.net/dashoumeixi/article/details/82901029
​
//test.h
template<class T> 
class A 
{ 
public: 
void bb(); //声明 
}; 

//test.cpp
#include”test.h” 
template<class T> 
void A<T>::bb() //实现
{ 

} 

//main.cpp
#include”test.h” 
int main() 
{ 
    A<int> a; 
    a. bb();
}

​以上将链接出错.
模版规定,只有使用到的时候才会实例化.
虽然test.cpp有实现代码,但并不生成代码.可以看看test.o,原因是在test.cpp中没有使用到A<int>...;
main.cpp 包含了.h文件全是声明, 编译器期望链接器能在别的文件.o 中能找到相关定义.可惜test.o中没有

猜你喜欢

转载自blog.csdn.net/dashoumeixi/article/details/82901029
今日推荐