C++模板类的编译问题
2013-09-23 17:03
232 查看
C++模板类的编译问题
2009-01-21 00:48:56
标签:C++ 模板类 休闲 职场
一般发布C++类的方法是:使用.H文件写类定义,用CPP文件写实现。发布的时候可以把H文件和CPP编译出来的.O文件给客户程序,然后客户端程序来编译和链接类头文件和目标文件。
虽然也可以发布CPP源文件,但发布.O文件可以更好的维护类的相对独立性,或者是源代码实现不方便让他人看到。
例如,
有类的头文件:
//Rec.h
#ifndef _REC_H_
#define _REC_H_
class Rec
{
public:
Rec(int a);
void show();
private:
int m_a;
};
#endif
类的实现文件。
//Rec.cpp
#include "Rec.h"
#include <iostream>
using namespace std;
Rec::Rec(int a)
{
m_a = a;
}
void Rec::show(){
printf("item=%d\n",m_a);
}
使用类的时候,首先 g++ -c Rec.cpp 生成Rec.o备用。
编写测试程序
//test.cpp
#include "Rec.h"
void main()
{
Rec a(10);
a.show();
}
编译测试程序, g++ -o test test.cpp Rec.o
运行得: item=10
如果把Rec类变成模板类,那么编译的方法就不能如此优雅了。
Rect类头文件
//Rect.h
#ifndef _RECT_H_
#define _RECT_H_
template <class T>
class RecT{
public:
RecT(T t);
void show();
private:
T m_a;
};
#endif //! #ifndef _RECT_H_
Rect类实现文件:
#Rect.cpp
#include "RecT.h"
#include <iostream>
using namespace std;
template <class T>
RecT<T>::RecT(T t)
{
m_a = t;
}
template <class T>
void RecT<T>::show(){
cout<<"item="<<m_a<<endl;
}
测试程序:使用模板类,T用int代替
#include "RecT.h"
void main()
{
RecT<int> a(10);
a.show();
}
在VS6下,编译出现错误:
Compiling...
Test.cpp
Linking...
Test.obj : error LNK2001: unresolved external symbol "public: void __thiscall RecT<int>::show(void)" (?show@?$RecT@H@@QAEXXZ)
Test.obj : error LNK2001: unresolved external symbol "public: __thiscall RecT<int>::RecT<int>(int)" (??0?$RecT@H@@QAE@H@Z)
Debug/TemplateTest.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.
错误分析:
Test.cpp 在编译目标文件是成功的,在链接的时候出现了错误。找不到RecT<int>类的方法。
事实上,Rect.cpp无法独立生成目标文件。理由很简单:因为其中的T类型在使用前还未知类型,如何分配内存还无法确定,自然就无法生成目标文件了。
所以,编译模板类不能分两步走。模板类的实现文件需要和使用类的程序一起编译,编译器知道程序需要使用何种类后才可以编译出相应的类来。
改改测试程序
#include "RecT.cpp"
void main()
{
RecT<int> a(10);
a.show();
}
编译通过并成功运行
相关文章推荐
- [转]C++模板类的编译问题
- C++模板类(方法)分离编译问题
- 数据结构与算 5:C++ 顺序/链式存储,栈 模板类实现,编译模板类问题解决
- C++模板类的问题 map::iterator 编译报错的问题
- C++模板类的编译问题
- 使用C/C++编译预处理时需要注意的问题(宏相关问题)
- C++模板的编译问题
- c++模板类学习--例子编译可能有问题【注】
- C++编译遇到问题汇总
- C++程序编译运行后窗口一闪而过问题
- [Android-NDK编译] ndk 编译 c++ 兼容性问题汇总整理
- 在windows下的qt5.7.1 的纯C或C++项目命令行中文乱码、无法编译等问题解决
- C++编译时出现的一个问题,求教高人
- C++多文件编译问题
- C++模板类 容器之map及对象副本 深/浅拷贝等问题浅析
- 使用SSD目标检测c++接口编译问题解决记录
- c++模板类(一)理解编译器的编译模板过程
- C++编译问题
- c/c++编译问题
- 关于sublime 编译C与gc++编译C的一个问题