• <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
            无码专区久久综合久中文字幕| 国产精品久久久久久福利漫画| 久久综合视频网站| 日本欧美国产精品第一页久久| 久久这里的只有是精品23| 亚洲精品乱码久久久久久蜜桃不卡| 久久国产精品成人片免费| 精品国产综合区久久久久久| 久久亚洲国产精品成人AV秋霞| 久久精品国产亚洲AV无码娇色 | 久久人人爽人人爽人人AV| 东方aⅴ免费观看久久av| 久久精品国产91久久麻豆自制| 久久久精品波多野结衣| 久久久av波多野一区二区| 久久亚洲av无码精品浪潮| 精品国产乱码久久久久久1区2区 | 99国产精品久久| 四虎影视久久久免费观看| 久久综合丁香激情久久| 久久久精品2019免费观看| 午夜视频久久久久一区| 国内精品久久久久久久久| 精品久久久久久国产潘金莲| 国产精品久久婷婷六月丁香| 国产亚洲精久久久久久无码AV| 久久午夜伦鲁片免费无码| 综合久久给合久久狠狠狠97色| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久精品女人天堂AV麻| 国内精品久久人妻互换| 亚洲午夜久久久久久久久电影网| 性欧美大战久久久久久久| 99久久综合国产精品二区| 99久久人妻无码精品系列蜜桃| 日韩精品久久久肉伦网站| 久久亚洲国产成人精品性色| 久久精品国产男包| 久久久久99这里有精品10| 伊人久久精品影院| 久久人做人爽一区二区三区|