• <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年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            享受編程

            常用鏈接

            留言簿(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 漂漂 閱讀(1160) 評論(0)  編輯 收藏 引用 所屬分類: visual studio
            成人资源影音先锋久久资源网| 久久国产精品99国产精| 色综合久久最新中文字幕| 久久99精品久久久久久9蜜桃 | 国产欧美一区二区久久| 国产精品狼人久久久久影院| 亚洲v国产v天堂a无码久久| 久久精品中文无码资源站| 亚洲国产天堂久久综合网站| 青草久久久国产线免观| 欧美噜噜久久久XXX| 久久精品无码一区二区三区日韩| 欧美性大战久久久久久| 99国产欧美久久久精品蜜芽| 日本精品久久久久久久久免费| 久久SE精品一区二区| 久久国产三级无码一区二区| 国内精品久久久久影院薰衣草| 狠狠色丁香婷综合久久| 久久精品国产亚洲av麻豆蜜芽| 韩国三级大全久久网站| 久久久久亚洲AV片无码下载蜜桃| 久久久久婷婷| 久久精品女人天堂AV麻| 狠狠综合久久综合中文88| 久久99精品综合国产首页| 精品免费久久久久久久| 亚洲va久久久噜噜噜久久狠狠| 日本精品久久久久久久久免费| 国产精品久久久天天影视香蕉| 99久久人妻无码精品系列蜜桃| 一本色道久久综合狠狠躁 | 亚洲国产欧洲综合997久久| 免费精品久久久久久中文字幕 | 亚洲AⅤ优女AV综合久久久| 久久AⅤ人妻少妇嫩草影院| 国产精品热久久毛片| 久久国产成人午夜aⅴ影院 | 久久噜噜电影你懂的| 久久国产精品-国产精品| 国产成人综合久久久久久|