• <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
            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(2)

            隨筆檔案(7)

            相冊

            搜索

            •  

            積分與排名

            • 積分 - 15805
            • 排名 - 952

            最新評論

            閱讀排行榜

            評論排行榜

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

            #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; // 指向第一個節(jié)點(diǎn)的指針
            };

            #endif? // _CHAIN

            結(jié)果報錯:
            --------------------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)

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

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

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

            FeedBack:
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 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> 在沒有聲明之前
            // 不被發(fā)現(xiàn)。。。
            // 因?yàn)?friend class Chain<T>
            // 不是聲明 Chain 是類模板。
            // -- 以上個人之見。
            private:
            T data;
            ChainNode<T> *link;
            };

              回復(fù)  更多評論
              
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 2006-06-18 21:59 Corner
            是啊!缺了template<class T> class Chain;的前向聲明在template<class T> struct ChainNode前面.  回復(fù)  更多評論
              
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 2006-06-19 09:59 Bourne
            試了一下,果然是這個錯誤,謝謝朋友的指點(diǎn)??!  回復(fù)  更多評論
              
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 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; // 指向第一個節(jié)點(diǎn)的指針
            };   回復(fù)  更多評論
              
            久久精品国产精品亚洲毛片 | 亚洲精品美女久久777777| 欧美久久久久久精选9999| 热久久国产欧美一区二区精品| 日韩欧美亚洲综合久久影院Ds | 久久AV高潮AV无码AV| 99久久综合狠狠综合久久止| 曰曰摸天天摸人人看久久久| 一本久道久久综合狠狠躁AV| 国产午夜免费高清久久影院| 久久综合色之久久综合| 久久香综合精品久久伊人| 久久精品这里只有精99品| 精品乱码久久久久久久| 久久天天躁狠狠躁夜夜2020老熟妇| 欧美亚洲国产精品久久高清| 88久久精品无码一区二区毛片| 久久国产欧美日韩精品免费| 欧美一区二区精品久久| 亚洲性久久久影院| 久久天天躁狠狠躁夜夜av浪潮| 97久久久久人妻精品专区| 精品综合久久久久久97| 精品乱码久久久久久夜夜嗨| 99久久国产综合精品麻豆| 亚洲va久久久噜噜噜久久男同| 蜜桃麻豆www久久国产精品| 女人香蕉久久**毛片精品| 久久综合给合久久狠狠狠97色69| 久久精品国产只有精品66| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久久国产99久久国产一| 久久国产成人午夜AV影院| 免费观看成人久久网免费观看| 99精品久久精品| MM131亚洲国产美女久久| 国产精品一区二区久久不卡| 久久久av波多野一区二区| 无码伊人66久久大杳蕉网站谷歌 | 久久综合给久久狠狠97色| 久久AV高清无码|