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

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            微軟VC++對友元類和模板的支持真的這么差嗎?

            以前寫代碼的時候就遇到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; // 指向第一個節點的指針
            };

            #endif  // _CHAIN

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

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

            反正結構體中的數據成員都是public的,至于訪問限制的實現就依靠迭代器來實現了,g++的STL中的樹結點不也是結構體嗎?:)

            posted on 2006-06-17 23:39 Bourne 閱讀(733) 評論(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> 在沒有聲明之前
            // 不被發現。。。
            // 因為 friend class Chain<T>
            // 不是聲明 Chain 是類模板。
            // -- 以上個人之見。
            private:
            T data;
            ChainNode<T> *link;
            };

              回復  更多評論
             
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 2006-06-18 21:59 Corner
            是??!缺了template<class T> class Chain;的前向聲明在template<class T> struct ChainNode前面.  回復  更多評論
             
            # re: 微軟VC++對友元類和模板的支持真的這么差嗎? 2006-06-19 09:59 Bourne
            試了一下,果然是這個錯誤,謝謝朋友的指點?。?nbsp; 回復  更多評論
             
            # 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; // 指向第一個節點的指針
            };   回復  更多評論

            posted on 2008-08-31 21:58 肥仔 閱讀(879) 評論(0)  編輯 收藏 引用 所屬分類: C++ 模板

            久久久久久久综合日本| 国产精品一久久香蕉国产线看| 99久久精品免费观看国产| 精品国产热久久久福利| 亚洲人成无码www久久久| 亚洲国产精品无码久久98| 久久狠狠色狠狠色综合| 亚洲精品无码久久不卡| 97r久久精品国产99国产精| 99久久久久| 久久精品国产清高在天天线| 国产成人久久久精品二区三区| 欧美日韩久久中文字幕| 天天爽天天爽天天片a久久网| 久久综合久久美利坚合众国| 久久福利青草精品资源站| 久久久久久国产精品美女| 久久综合综合久久狠狠狠97色88| 伊人久久大香线蕉综合5g| 天天综合久久久网| 久久天堂AV综合合色蜜桃网| 一本大道久久东京热无码AV| 久久久久久久尹人综合网亚洲| 最新久久免费视频| 欧美性猛交xxxx免费看久久久| 久久精品国产半推半就| 欧美午夜精品久久久久免费视 | 久久久久九九精品影院| 久久精品夜夜夜夜夜久久| 国内精品伊人久久久久妇| 性欧美大战久久久久久久| 热久久国产欧美一区二区精品| 狠狠人妻久久久久久综合蜜桃| 久久精品国产精品国产精品污| 无码人妻久久久一区二区三区| 免费一级做a爰片久久毛片潮 | 久久永久免费人妻精品下载| 久久无码AV中文出轨人妻| 久久人人爽人人爽人人片av麻烦 | 久久强奷乱码老熟女网站| 国产精品美女久久久久av爽|