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

            我輩豈是蓬蒿人!

            C++ && keyWordSpotting

              C++博客 :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
              11 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(9)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 7081
            • 排名 - 1377

            最新評論

            閱讀排行榜

            評論排行榜

            ???學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu),用到了輸入輸出運算符重載,結(jié)果編譯之下,錯誤重重,,隨即停止學(xué)習(xí)進(jìn)度,追查禍源,在花費巨大腦力與時間成本之后,終于知自己錯誤之所在,定位于名字空間之困擾。為牢記教訓(xùn),寫一簡化版本記錄 debug 過程如下,警示自己。

            問題
            ???

            ???有如下兩個代碼文件:
            ???// 20060816_operator.cxx

            ?1?#include?<iostream>
            ?2?using?namespace?std;
            ?3?#include?"20060816_operator.h"
            ?4?int?main(int?argc,?char*?argv[])
            ?5?{
            ?6?????const?std::string?GHH("GuoHonghua");
            ?7?????Honghua?ghh(GHH);
            ?8?????cout?<<?ghh?<<?endl;
            ?9?????return?0;
            10?}
            ???

            // 20060816_operator.h

            ?1?#ifndef?_20060816_OPERATOR_GHH_
            ?2?#define?_20060816_OPERATOR_GHH_????1
            ?3?
            ?4?//?Author?:?GuoHonghua
            ?5?//?Date?:?2006.08.16
            ?6?//?File?:?20060816_operator.h
            ?7?
            ?8?#include?<iostream>
            ?9?#include?<string>
            10?class?Honghua
            11?{
            12?????friend?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh);
            13?public:
            14?????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
            15?????{
            16?????}
            17?
            18?private:
            19?????std::string?_ghh;
            20?};
            21?
            22?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh)
            23?{
            24?????os?<<?ghh._ghh;
            25?????return?os;
            26?}
            27?
            28?#endif?//?end?of?_20060816_OPERATOR_GHH_

            用上面兩個文件建立工程,vc6.0下編譯會出錯。報告如下:

            --------------------Configuration: 20060816_operator - Win32 Debug--------------------

            Compiling...

            20060816_operator.cxx

            f:\ghh_project\cxxdetail\20060816_operator.h(24) : error C2248: '_ghh' : cannot access private member declared in class 'Honghua'

            ??????? f:\ghh_project\cxxdetail\20060816_operator.h(19) : see declaration of '_ghh'

            F:\ghh_project\CxxDetail\20060816_operator.cxx(8) : error C2593: 'operator <<' is ambiguous

            Error executing cl.exe.

            ?

            20060816_operator.exe - 2 error(s), 0 warning(s)
            解決:
            ???

            1.? 調(diào)整名字空間聲明和自定義頭文件的次序,如下:

            1?#include?<iostream>
            2?#include?"20060816_operator.h"
            3?using?namespace?std;
            4?int?main(int?argc,?char*?argv[])
            5?

            2.? 把類定義于標(biāo)準(zhǔn)名字之內(nèi),20060816_operator.h文件修改如下(不推薦)

            ?1?#ifndef?_20060816_OPERATOR_GHH_
            ?2?#define?_20060816_OPERATOR_GHH_????1
            ?3?
            ?4?//?Author?:?GuoHonghua
            ?5?//?Date?:?2006.08.16
            ?6?//?File?:?20060816_operator.h
            ?7?
            ?8?#include?<iostream>
            ?9?#include?<string>
            10?namespace?std
            11?{
            12?????class?Honghua
            13?????{
            14?????????friend?std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh);
            15?????public:
            16?????????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
            17?????????{
            18?????????}
            19?????????
            20?????private:
            21?????????std::string?_ghh;
            22?????};
            23?????
            24?????
            25?????std::ostream&?operator<<(std::ostream&?os,?const?Honghua&?ghh)
            26?????{
            27?????????os?<<?ghh._ghh;
            28?????????return?os;
            29?????}
            30?}
            31?#endif?//?end?of?_20060816_OPERATOR_GHH_?

            3.? 使用頭文件iostream.h代替iostream,比較落后的方式(不推薦!

            ?1?//?20060816_operator.cxx
            ?2?//?#include?<iostream>
            ?3?#include?<iostream.h>
            ?4?#include?<iostream.h>
            ?5?#include?"20060816_operator.h"
            ?6?int?main(int?argc,?char*?argv[])
            ?7?{
            ?8?????const?std::string?GHH("GuoHonghua");
            ?9?????Honghua?ghh(GHH);
            10?????cout?<<?ghh?<<?endl;
            11?????return?0;
            12?}?

            ?1?//?20060816_operator.h
            ?2?#ifndef?_20060816_OPERATOR_GHH_
            ?3?#define?_20060816_OPERATOR_GHH_????1
            ?4?
            ?5?//?Author?:?GuoHonghua
            ?6?//?Date?:?2006.08.16
            ?7?//?File?:?20060816_operator.h
            ?8?//?#include?<iostream>
            ?9?#include?<iostream.h>
            10?#include?<string>
            11?
            12?class?Honghua
            13?{
            14?????friend?ostream&?operator<<(ostream&?os,?const?Honghua&?ghh);
            15?public:
            16?????Honghua(const?std::string&?ghh)?:?_ghh(ghh)
            17?????{
            18?????}
            19?????
            20?private:
            21?????std::string?_ghh;
            22?};
            23?
            24?
            25?ostream&?operator<<(ostream&?os,?const?Honghua&?ghh)
            26?{
            27?????os?<<?ghh._ghh;
            28?????return?os;
            29?}
            30?
            31?#endif?//?end?of?_20060816_OPERATOR_GHH_

            4.? 任何時候都不使用using namespace 聲明!這是只需要修改20060816_operator.cxx文件如下:

            ?1?#include?<iostream>
            ?2?#include?"20060816_operator.h"
            ?3?int?main(int?argc,?char*?argv[])
            ?4?{
            ?5?????//?const?string?GHH("GuoHonghua");
            ?6?????const?std::string?GHH("GuoHonghua");
            ?7?????Honghua?ghh(GHH);
            ?8?????//?cout?<<?ghh?<<?endl;
            ?9?????std::cout?<<?ghh?<<?std::endl;
            10?????return?0;
            11?}

            體會
            ???名字空間本來就是為了解決名字沖突問題而引入,以使標(biāo)準(zhǔn)庫不受外界影響。在這個意義上來說,
            using namespace std;不是可以隨意使用的語句,應(yīng)該考慮到它的位置對程序的可能影響,而最徹底的杜絕錯誤的解決辦法是在任何時候任何情況下都不使用此語句!

            posted on 2006-08-16 21:09 keyws 閱讀(402) 評論(0)  編輯 收藏 引用 所屬分類: 程序調(diào)試
            日韩一区二区久久久久久| 99精品国产99久久久久久97| 九九久久99综合一区二区| 国产精品久久亚洲不卡动漫| 成人精品一区二区久久| 午夜精品久久久久9999高清| 草草久久久无码国产专区| 久久人人爽人人精品视频 | av国内精品久久久久影院| 99久久国产主播综合精品| 日本五月天婷久久网站| 97久久香蕉国产线看观看| 久久久精品久久久久久| 粉嫩小泬无遮挡久久久久久| 武侠古典久久婷婷狼人伊人| 97久久精品人妻人人搡人人玩| 久久久久国产精品麻豆AR影院| 久久亚洲私人国产精品| 2021国产精品久久精品| 久久综合久久久| 久久久久久久亚洲Av无码| 性欧美大战久久久久久久| 亚洲一区中文字幕久久| 久久er99热精品一区二区| 久久精品国产男包| 一级a性色生活片久久无少妇一级婬片免费放| 婷婷久久久亚洲欧洲日产国码AV | 国产精品美女久久久久AV福利| 狠狠综合久久综合88亚洲| 伊色综合久久之综合久久| 国产激情久久久久影院老熟女| 99久久免费国产特黄| 国产亚洲综合久久系列| 久久偷看各类wc女厕嘘嘘| 亚洲国产精品无码成人片久久| 国产69精品久久久久APP下载| 三级韩国一区久久二区综合 | 久久天天躁狠狠躁夜夜96流白浆 | 国产精品欧美久久久久天天影视 | 77777亚洲午夜久久多喷| 久久99精品国产麻豆|