• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 224  文章 - 41  trackbacks - 0
            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            享受編程

            常用鏈接

            留言簿(11)

            隨筆分類(159)

            隨筆檔案(224)

            文章分類(2)

            文章檔案(4)

            經(jīng)典c++博客

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

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

                  一直寫程序都不習(xí)慣使用類,有幾次使用類分離都出現(xiàn)連接錯(cuò)誤,苦苦找不到原因,在Baidu海搜還是一無(wú)所獲,最近做數(shù)據(jù)結(jié)構(gòu)實(shí)驗(yàn)報(bào)告又碰到這個(gè)錯(cuò)誤,搜了很久,并配合課本的例子,終于找到了錯(cuò)誤的原因。

            1、當(dāng)類模板的定義及實(shí)現(xiàn)分離(即寫在不同文件中)時(shí),在其他文件中包含類模板的定義必須包含其類實(shí)現(xiàn)文件(即.cpp文件),而不能包含類定義文件(即.h文件),否則將收到“error LNK2001”錯(cuò)誤。

            2、若是一般的類(即不是模板類),在其他文件包含則必須包含頭文件(即.h文件,類聲明文件),而不能包含源文件(即.cpp文件,類實(shí)現(xiàn)文件),否則將收到“error LNK2005”錯(cuò)誤。

            如:

            ///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"         //這里應(yīng)該為#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;

            }

            以上程序?qū)⑹盏饺缦洛e(cuò)誤:

            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"         //這里應(yīng)改為#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;

            }

            如上程序?qū)⑹盏饺缦洛e(cuò)誤:

            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 漂漂 閱讀(1159) 評(píng)論(0)  編輯 收藏 引用 所屬分類: visual studio
            久久精品国产免费观看三人同眠| 国产日产久久高清欧美一区| 久久久久久毛片免费播放| 午夜精品久久久内射近拍高清| 国产精品美女久久久久av爽 | 国产亚洲成人久久| 国产精品久久亚洲不卡动漫| 久久久久亚洲精品天堂| 久久精品国产亚洲AV高清热| 久久国产精品77777| 久久免费视频观看| 国内精品久久久久久久久| 久久涩综合| 久久久久免费精品国产| 午夜精品久久久久久久| 国产精品久久一区二区三区| 国产伊人久久| 久久人人爽人人爽人人片AV麻烦| 亚洲中文精品久久久久久不卡| 久久精品人人做人人妻人人玩| 久久综合欧美成人| 亚洲国产日韩欧美久久| 久久99精品久久只有精品| 国产免费久久精品丫丫| 精品国产乱码久久久久久呢| 久久精品一区二区国产| 久久中文精品无码中文字幕| 浪潮AV色综合久久天堂| 久久强奷乱码老熟女网站| 日韩精品久久久肉伦网站| 国产成人无码精品久久久久免费| 亚洲欧洲中文日韩久久AV乱码| 久久久久人妻精品一区| 精品久久久无码中文字幕天天| 伊人色综合九久久天天蜜桃| 狠狠久久亚洲欧美专区| 精品久久久无码21p发布| 久久久久亚洲av成人无码电影| 久久久久久午夜成人影院 | 久久夜色精品国产噜噜麻豆| 办公室久久精品|