随笔 - 224  文章 - 41  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

原文:http://lnkm.2006.blog.163.com/blog/static/31474774200843181419322/

      一直写程序都不习惯使用类,有几次使用类分离都出现连接错误,苦苦找不到原因,在Baidu海搜还是一无所获,最近做数据结构实验报告又碰到这个错误,搜了很久,并配合课本的例子,终于找到了错误的原因。

1、当类模板的定义及实现分离(即写在不同文件中)时,在其他文件中包含类模板的定义必须包含其类实现文件(即.cpp文件),而不能包含类定义文件(即.h文件),否则将收到“error LNK2001”错误。

2、若是一般的类(即不是模板类),在其他文件包含则必须包含头文件(即.h文件,类声明文件),而不能包含源文件(即.cpp文件,类实现文件),否则将收到“error LNK2005”错误。

如:

///test.h文件

#ifndef HH

#define HH

class CTest

{

public:

CTest();

~CTest();

int get();

void set(int d);

private:

int num;

};

#endif

///test.cpp文件

#include"lll.h"

CTest::CTest()

{

};

CTest::~CTest()

{

};

int CTest::get()

{

return num;

}

void CTest::set(int d)

{

num=d;

}

///main文件

#include"test.cpp"         //这里应该为#include"test.h"

#include<iostream>

using namespace std;

int main()

{

CTest tt;

int n;

cin>>n;

tt.set(n);

cout<<tt.get()<<endl;

return 0;

}

以上程序将收到如下错误:

main.obj : error LNK2005: "public: __thiscall CTest::CTest(void)" (??0clll@@QAE@XZ) already defined in lll.obj

main.obj : error LNK2005: "public: __thiscall CTest::~CTest(void)" (??1clll@@QAE@XZ) already defined in lll.obj

main.obj : error LNK2005: "public: int __thiscall CTest::get(void)" (?get@clll@@QAEHXZ) already defined in lll.obj

main.obj : error LNK2005: "public: void __thiscall CTest::set(int)" (?set@clll@@QAEXH@Z) already defined in lll.obj

Debug/tt.exe : fatal error LNK1169: one or more multiply defined symbols found

Error executing link.exe.

又如:

///test文件:

#ifndef HH

#define HH

template<class T>

class CTest

{

public:

CTest();

~CTest();

T get();

void set(T d);

private:

T num;

};

#endif

///test.cpp文件

#include"test.h"

template<class T>

CTest<T>::CTest()

{

};

template<class T>

CTest<T>::~CTest()

{

};

template<class T>

T CTest<T>::get()

{

return num;

}

template<class T>

void CTest<T>::set(T d)

{

num=d;

}

///main文件:

#include"test.h"         //这里应改为#include"test.cpp"

#include<iostream>

using namespace std;

int main()

{

CTest<int> tt;

int n;

cin>>n;

tt.set(n);

cout<<tt.get()<<endl;

return 0;

}

如上程序将收到如下错误:

main.obj : error LNK2001: unresolved external symbol "public: __thiscall CTest<int>::~CTest<int>(void)" (??1?$clll@H@@QAE@XZ)

main.obj : error LNK2001: unresolved external symbol "public: int __thiscall CTest<int>::get(void)" (?get@?$clll@H@@QAEHXZ)

main.obj : error LNK2001: unresolved external symbol "public: void __thiscall CTest<int>::set(int)" (?set@?$clll@H@@QAEXH@Z)

main.obj : error LNK2001: unresolved external symbol "public: __thiscall CTest<int>::CTest<int>(void)" (??0?$clll@H@@QAE@XZ)

Debug/tt.exe : fatal error LNK1120: 4 unresolved externals

Error executing link.exe.

posted on 2008-11-20 16:34 漂漂 阅读(1119) 评论(0)  编辑 收藏 引用 所属分类: visual studio

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理