• <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)

            相冊(cè)

            搜索

            •  

            積分與排名

            • 積分 - 15767
            • 排名 - 948

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            以前寫代碼的時(shí)候就遇到VC++對(duì)友元支持得不太好的問(wèn)題,同時(shí)也看過(guò)侯捷老師對(duì)gnu c++, VC++, BCB 三種編譯器的比較,其中VC++對(duì)模板友元的支持就不是很好。
            今天晚上寫了一個(gè)比較簡(jiǎn)單的鏈表的模板類,其中頭文件Chain.h原來(lái)的代碼如下:

            #include <iostream>
            using namespace std;

            #ifndef _CHAIN
            #define _CHAIN

            template<class T>
            class?ChainNode
            {
            ?friend class Chain<T>;
            private:
            ?T data;
            ?ChainNode<T> *link;
            };

            template<class T>
            class Chain{
            public:
            ?Chain()
            ?{
            ??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)的指針
            };

            #endif? // _CHAIN

            結(jié)果報(bào)錯(cuò):
            --------------------Configuration: Chain - Win32 Debug--------------------
            Compiling...
            Chain.cpp
            g:\work plan\c++ code practice\chain\chain.h(17) : error C2059: syntax error : '<'
            ??????? g:\work plan\c++ code practice\chain\chain.h(21) : see reference to class template instantiation 'ChainNode<T>' being compiled
            g:\work plan\c++ code practice\chain\chain.h(17) : error C2238: unexpected token(s) preceding ';'
            ??????? g:\work plan\c++ code practice\chain\chain.h(21) : see reference to class template instantiation 'ChainNode<T>' being compiled
            g:\work plan\c++ code practice\chain\chain.h(40) : error C2989: 'Chain' : template class has already been defined as a non-template class
            ??????? g:\work plan\c++ code practice\chain\chain.h(17) : see declaration of 'Chain'
            g:\work plan\c++ code practice\chain\chain.cpp(6) : error C2059: syntax error : '<'
            g:\work plan\c++ code practice\chain\chain.cpp(6) : error C2588: '::~Chain' : illegal global destructor
            g:\work plan\c++ code practice\chain\chain.cpp(6) : fatal error C1903: unable to recover from previous error(s); stopping compilation
            Error executing cl.exe.

            Chain.obj - 6 error(s), 0 warning(s)

            感覺(jué)從代碼來(lái)看應(yīng)該是沒(méi)有問(wèn)題的,如果哪個(gè)高手看出問(wèn)題來(lái)了請(qǐng)一定告訴我啊,如果知道編譯不通過(guò)的原因也請(qǐng)一定要告訴我啊。沒(méi)辦法,最后采用解決的辦法就是修改ChainNode的定義了,定義為結(jié)構(gòu)體:)
            template<class T>
            struct ChainNode
            {
            ? T data;
            ? ChainNode<T> *link;
            };

            反正結(jié)構(gòu)體中的數(shù)據(jù)成員都是public的,至于訪問(wèn)限制的實(shí)現(xiàn)就依靠迭代器來(lái)實(shí)現(xiàn)了,g++的STL中的樹(shù)結(jié)點(diǎn)不也是結(jié)構(gòu)體嗎?:)

            posted on 2006-06-17 23:39 Bourne 閱讀(2643) 評(píng)論(4)  編輯 收藏 引用

            FeedBack:
            # re: 微軟VC++對(duì)友元類和模板的支持真的這么差嗎? 2006-06-18 15:01 mroske
            // 在 ChainNode 之前聲明 Chain 。
            template<class T>
            class Chain;

            template<class T>
            struct ChainNode
            {
            friend class Chain<T>; // class Chain<T> 在沒(méi)有聲明之前
            // 不被發(fā)現(xiàn)。。。
            // 因?yàn)?friend class Chain<T>
            // 不是聲明 Chain 是類模板。
            // -- 以上個(gè)人之見(jiàn)。
            private:
            T data;
            ChainNode<T> *link;
            };

              回復(fù)  更多評(píng)論
              
            # re: 微軟VC++對(duì)友元類和模板的支持真的這么差嗎? 2006-06-18 21:59 Corner
            是啊!缺了template<class T> class Chain;的前向聲明在template<class T> struct ChainNode前面.  回復(fù)  更多評(píng)論
              
            # re: 微軟VC++對(duì)友元類和模板的支持真的這么差嗎? 2006-06-19 09:59 Bourne
            試了一下,果然是這個(gè)錯(cuò)誤,謝謝朋友的指點(diǎn)啊!  回復(fù)  更多評(píng)論
              
            # re: 微軟VC++對(duì)友元類和模板的支持真的這么差嗎? 2008-03-11 02:12 WR

            #include <iostream.h>

            template <typename T>
            class Chain;

            template<class T>
            class ChainNode
            {
            friend class Chain<T>;
            private:
            T data;
            ChainNode<T> *link;
            };

            template<class T>
            class Chain{
            public:
            Chain()
            {
            // 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)的指針
            };   回復(fù)  更多評(píng)論
              

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


            99精品国产免费久久久久久下载| 久久亚洲国产午夜精品理论片| 国产农村妇女毛片精品久久| 丁香五月综合久久激情| 亚洲国产精品热久久| 久久中文字幕精品| 国产精品久久久久影视不卡 | 国产999精品久久久久久| 国产精品久久久久乳精品爆| 伊色综合久久之综合久久| 久久精品国产亚洲AV无码娇色| 国产免费福利体检区久久| 欧美精品国产综合久久| 久久精品一区二区三区不卡| 亚洲?V乱码久久精品蜜桃| 精品久久久久久无码专区| 四虎国产精品成人免费久久| 久久99精品国产麻豆宅宅| 无码国内精品久久人妻| 人妻精品久久久久中文字幕| 国产精品久久久久久影院 | 久久中文字幕人妻丝袜| 精品久久久久久久久久中文字幕 | 久久免费国产精品一区二区| 久久久久久久综合综合狠狠| 久久综合给久久狠狠97色| 欧美伊人久久大香线蕉综合| 久久久久国产精品麻豆AR影院 | 久久精品国产欧美日韩| 精品国产一区二区三区久久| 人人狠狠综合久久88成人| 国产精品99久久久精品无码| 中文字幕久久亚洲一区| 亚洲日本久久久午夜精品| 青青草国产97免久久费观看| 久久久久久亚洲精品无码| 9999国产精品欧美久久久久久| 青青草国产精品久久久久| 狠狠色丁香久久综合婷婷| 久久国产乱子精品免费女| 久久99精品国产一区二区三区|