您的位置:首页 > 编程语言 > C语言/C++

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();

}

编译通过并成功运行
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: