• <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>
            隨筆 - 7  文章 - 15  trackbacks - 0
            <2006年6月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            常用鏈接

            留言簿(2)

            隨筆檔案(7)

            相冊

            搜索

            •  

            積分與排名

            • 積分 - 15783
            • 排名 - 952

            最新評論

            閱讀排行榜

            評論排行榜

            還是我做的那個(gè)鏈表的模板類
            我分別寫了頭文件Chain.h,源文件Chain.cpp,并且在main.cpp中進(jìn)行測試。
            但是在連接的時(shí)候出現(xiàn)了問題,不知道什么原因,希望能夠有高手指點(diǎn)一下,非常感謝!

            其中Chain.h聲明了類的數(shù)據(jù)成員和成員函數(shù),具體內(nèi)容如下:

            #ifndef _CHAIN_H
            #define _CHAIN_H

            #include <iostream>
            using namespace std;

            template<class T>
            class Chain;


            template<class T>
            class ChainNode
            {
            ?friend class Chain<T>;
            ?friend ostream& operator<<(ostream& out, const Chain<T>& x);
            private:
            ?T data;
            ?ChainNode<T> *link;
            };

            template<class T>
            class Chain{

            public:
            ?Chain(int p) {first = 0;};
            ?~Chain();
            ?bool IsEmpty() const {return first == 0;}
            ?int Length() const;
            ?bool Find(int k, T& x) const;
            ?int Search(const T& x) const;
            ?//Chain<T>& Delete(int k, T& x);
            ?Chain<T>& Insert(int k, const T& x);
            ?void Output(ostream& out = cout) const;
            private:
            ?ChainNode<T> *first; // 指向第一個(gè)節(jié)點(diǎn)的指針
            };

            template<class T>
            ostream& operator<<(ostream& out, const Chain<T>& x);

            #endif? // _CHAIN

            ??在Chain.cpp中對Chain.h中聲明的函數(shù)進(jìn)行了定義,具體內(nèi)容如下:

            #include "Chain.h"
            #include <iostream>
            using namespace std;

            template<class T>
            Chain<T>::~Chain()
            {
            ?//鏈表的析構(gòu)函數(shù),用于刪除鏈表中的所有節(jié)點(diǎn)
            ?ChainNode<T> *ptr = first;
            ?while (ptr)
            ?{
            ??first = ptr->link;
            ??delete ptr;
            ??ptr = first;
            ?}
            }

            template<class T>
            int Chain<T>::Length() const
            {
            ?//返回鏈表中的元素總數(shù)
            ?int count = 0;
            ?ChainNode<T> *ptr = first;
            ?while (ptr)
            ?{
            ??++count;
            ??ptr = ptr->link;
            ?}
            ?return count;
            }

            template<class T>
            bool Chain<T>::Find(int k, T& x) const
            {
            ?//尋找鏈表中的第k個(gè)元素,并將其傳送至x
            ?//如果不存在第k個(gè)元素,則返回false,否則返回true
            ?if (k < 1)
            ?{
            ??return false;
            ?}
            ?int count = k;
            ?ChainNode<T> *ptr = first;
            ?while (count && ptr)
            ?{
            ??--count;
            ??ptr = ptr->link
            ?}
            ?if (!ptr)
            ?{
            ??return false;
            ?}
            ?x = ptr->data;
            ?return true;
            }

            template<class T>
            int Chain<T>::Search(const T& x) const
            {
            ?//尋找x,如果發(fā)現(xiàn)x,則返回x的地址
            ?//如果x不在鏈表中,則返回0
            ?int count = 1;
            ?ChainNode<T> *ptr = first;
            ?while(ptr && (ptr->data!=x))
            ?{
            ??++count;
            ??ptr = ptr->link;
            ?}
            ?if (ptr)
            ?{
            ??return count;
            ?}
            ?else
            ?{
            ??return 0;
            ?}
            }

            template<class T>
            void Chain<T>::Output(ostream& out = cout) const
            {
            ?ChainNode<T> *ptr = first;
            ?while (ptr)
            ?{
            ??out<<ptr->data<<"? ";
            ??ptr = ptr->link;
            ?}
            }

            //重載<<運(yùn)算符
            template<class T>
            ostream& operator<<(ostream& out, const Chain<T>& x)
            {
            ?x.Output(out);
            ?return out;
            }

            template<class T>
            Chain<T>& Chain<T>::Insert(int k, const T& x)
            {
            ?ChainNode<T> *ptr = first;
            ?int count = 0;
            ?while (ptr && count < k)
            ?{
            ??++count;
            ??ptr = ptr->link;
            ?}
            ?if (!ptr)? //如果沒到第k個(gè)節(jié)點(diǎn)
            ?{
            ?
            ?}
            ?else
            ?{
            ??//要插入的新節(jié)點(diǎn)
            ??ChainNode<T>* cn = new ChainNode<T>;
            ??cn->data = x;
            ??cn->link = 0;
            ??if (ptr->link==0)? //到達(dá)了鏈表的結(jié)尾
            ??{
            ???ptr->link = cn;
            ??}
            ??else? //沒到達(dá)結(jié)尾
            ??{?
            ???cn->link = ptr->link;
            ???ptr->link = cn;
            ??}
            ?}
            ?return *this
            }

            ?在main.cpp中進(jìn)行測試,測試的內(nèi)容很少,但是剛開始就進(jìn)行不了了。main.cpp內(nèi)容如下:

            #include "Chain.h"
            using namespace std;

            int main()
            {
            ?Chain<int> c;
            ?cout<<c.Length();
            ?return 0;
            }

            編譯的時(shí)候沒有問題,但是在連接的時(shí)候就出現(xiàn)了問題,報(bào)錯(cuò)如下:
            --------------------Configuration: Chain - Win32 Debug--------------------
            Linking...
            main.obj : error LNK2001: unresolved external symbol "public: __thiscall Chain<int>::~Chain<int>(void)" (
            ??1?$Chain@H@@QAE@XZ)
            main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Chain<int>::Length(void)const " (
            ?Length@?$Chain@H@@QBEHXZ)
            Debug/Chain.exe : fatal error LNK1120: 2 unresolved externals
            Error executing link.exe.

            Chain.exe - 3 error(s), 0 warning(s)
            但是從報(bào)錯(cuò)信息來看,應(yīng)該是在main.cpp中沒有找到所用到的函數(shù) ~Chain<int>(void)和Length()的定義,在main.cpp中一共用到了三個(gè)函數(shù),構(gòu)造函數(shù)Chain(),但是構(gòu)造函數(shù)是在Chain.h中定義的,所以編譯器找到了其定義,但是另外兩個(gè)函數(shù)是在Chain.cpp中定義的,而且目前報(bào)錯(cuò)沒有找到,但是如果在main.cpp中引入#include "Chain.cpp"時(shí),編譯和連接就沒有問題,這就證實(shí)了原來的估計(jì)是沒有錯(cuò)的。我實(shí)在是不知道問題出現(xiàn)在哪里,所以希望哪位高手看出問題來的話,請告訴我,多謝了!

            posted on 2006-06-19 16:56 Bourne 閱讀(846) 評論(4)  編輯 收藏 引用

            FeedBack:
            # re: VC++中的連接問題,請大俠幫忙,我快崩潰了 2006-06-19 18:20 CoderDream
            將Chain.h 和 Chain.cpp 合并成一個(gè)文件,然后修改主函數(shù)中的第一句:
            Chain<int> c(666);//要帶參數(shù),不然沒有匹配的構(gòu)造函數(shù)

            但是運(yùn)行結(jié)果為0,顯然不對,再看看哪里出了問題。

            有模板時(shí)最好將函數(shù)的聲明和定義寫在同一個(gè)文件中  回復(fù)  更多評論
              
            # re: VC++中的連接問題,請大俠幫忙,我快崩潰了 2006-06-19 18:54 萬連文
            模板的實(shí)現(xiàn)最好在.h里面,如果非要象你那樣也可以,在.h里面包含.cpp。  回復(fù)  更多評論
              
            # re: VC++中的連接問題,請大俠幫忙,我快崩潰了 2006-06-19 20:01 笑笑生
            #include "Chain.h"->#include "Chain.cpp"
            using namespace std;

            int main()
            {
            Chain<int> c;
            cout<<c.Length();
            return 0;
            }  回復(fù)  更多評論
              
            # re: VC++中的連接問題,請大俠幫忙,我快崩潰了 2006-06-19 21:44 Bourne
            我采取在main.cpp直接引入Chain.cpp的方法了

            #include "Chain.h"->#include "Chain.cpp"
            多謝各位的幫助阿!  回復(fù)  更多評論
              

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            国产精品99久久久久久猫咪| 久久精品日日躁夜夜躁欧美| 久久综合久久综合九色| 久久久久亚洲AV无码永不| 99久久99久久精品国产片| 久久久精品国产| 精品永久久福利一区二区| 久久91这里精品国产2020| 亚洲七七久久精品中文国产| 人妻精品久久久久中文字幕一冢本| 久久99热国产这有精品| 国产69精品久久久久APP下载 | 亚洲中文久久精品无码ww16| 精品国产乱码久久久久久1区2区 | 久久久国产精品| 亚洲中文字幕无码久久精品1| 国产巨作麻豆欧美亚洲综合久久| 精品一二三区久久aaa片| 国内精品久久久久久久影视麻豆| 人妻精品久久无码区| 久久天天躁夜夜躁狠狠躁2022| 国产成人精品久久| 97久久超碰国产精品旧版| 青草久久久国产线免观| 秋霞久久国产精品电影院| 国产成人精品免费久久久久| 久久精品国产亚洲av麻豆蜜芽| 欧美激情精品久久久久久久九九九 | 久久精品国产亚洲Aⅴ香蕉| 国产69精品久久久久777| 久久久久亚洲av无码专区导航| 伊人久久大香线焦AV综合影院 | 久久综合给合久久狠狠狠97色69| 国内精品久久久久影院老司| 久久综合九色欧美综合狠狠| 久久av高潮av无码av喷吹| 精品国产婷婷久久久| 久久久久国产日韩精品网站| 久久精品国产精品亚洲人人| 亚洲精品无码久久不卡| 久久久久久久久66精品片|