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

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理
            https://www.cnblogs.com/zjutzz/p/4018816.html

            在C++中,有時候需要在不同文件中使用同一個變量。對于這類變量如果處理不當,很容易出現“multiple definition of... first defined here”的錯誤。

            例如,定義了如下3個文件:global.h, a.cpp, b.cpp

            //global.h: #ifndef _GLOBAL_H_ #define _GLOBAL_H_  const int a=1; int b;  #endif
            //a.cpp #include <iostream> #include <stdlib.h> #include "global.h"  using namespace std;  void test1() {      cout<<"test1"<<endl; }
            //b.cpp #include <iostream> #include <stdlib.h> #include "global.h"  using namespace std;  void test2() {     cout<<"test2"<<endl; }  void main() {   cout<<"hello world"<<endl; }

            執行編譯命令:

            g++ -o main a.cpp b.cpp

            提示錯誤為:

            [chris@zz jojo]g++ -o main a.cpp b.cpp
            /tmp/ccc7OcsO.o:(.bss+0x0): multiple definition of `b'
            /tmp/ccs7q2VA.o:(.bss+0x0):第一次在此定義

            出錯原因:a.cpp和b.cpp先分別被編譯為.o格式的目標文件,兩個目標文件再被鏈接器鏈接起來,這當中a.cpp和b.cpp分別進行了一次include,相當于global.h中的代碼重復出現了一次。因為a是const類型,所以重新定義也沒事;但是b只是普通變量,重復定義顯然不行。

            顯然,一個解決辦法是把b定義為const int類型。或者,定義成static int類型也行。

            還有一種解決方案,就是把global.h變為global.c文件,a.cpp和b.cpp中不再include它,但是編譯的時候把global.c也編譯進去,就可以了:

            g++ -o main global.c  a.cpp b.cpp

            再舉一個class相關的例子。比如有Body和Mouth兩個類,Body的greet方法會調用Mouth的say方法,而main函數中會調用全局變量body的greet方法。為了只是用一個body和一個mouth對象,可以這么寫:

            //body.h #ifndef BODY_H #define BODY_H  #include <mouth.h>  class Body { public:     Body();     ~Body();     void greet(); };  extern Body body;   #endif 
            //body.cpp #include <body.h>  Body::Body(){}  Body::~Body() {}  void Body::greet() {     mouth.say(); } 
            //mouth.h #ifndef MOUTH_H #define MOUTH_H  class Mouth { public:     Mouth();     ~Mouth();     void say(); };  extern Mouth mouth;   #endif 
            //mouth.cpp #include <mouth.h> #include <iostream>  using namespace std;  Mouth::Mouth() {}  Mouth::~Mouth() {}  void Mouth::say() {     cout << "Have a Nice day!" << endl; } 
            //class.cpp #include <body.h> #include <mouth.h>   Body body; Mouth mouth; 
            //main.cpp #include <iostream> #include <body.h> using namespace std;  int main() {     body.greet(); } 

            上面代碼中的include,雖然都是用的尖括號,但因為編譯時可以通過指定include路徑,不會出問題~
            編譯命令:

            g++ -I ./ mouth.cpp body.cpp class.cpp main.cpp -o main

            能夠正常運行。

            无码超乳爆乳中文字幕久久| 九九精品99久久久香蕉| 国产福利电影一区二区三区久久老子无码午夜伦不 | 狠狠色婷婷久久一区二区| 影音先锋女人AV鲁色资源网久久| 久久国产欧美日韩精品| 99久久精品免费| 久久久久人妻一区二区三区| 久久国产精品77777| 狠狠精品干练久久久无码中文字幕| 久久亚洲国产成人影院网站| 囯产极品美女高潮无套久久久| 久久亚洲欧美日本精品| 欧美日韩精品久久免费| 国产成人AV综合久久| 久久久久久亚洲Av无码精品专口 | 欧美久久综合九色综合| 久久久免费精品re6| 午夜精品久久久久久久无码| 久久精品国产只有精品2020| 久久久久亚洲精品日久生情 | 久久久久国产日韩精品网站| 国产精品久久免费| 伊人久久大香线蕉av不变影院| 久久国产香蕉一区精品| 久久久久久综合一区中文字幕 | 久久久久久夜精品精品免费啦| 久久中文字幕人妻熟av女| 人人狠狠综合久久亚洲高清| 精品久久久久一区二区三区 | 日韩欧美亚洲综合久久影院Ds | 久久久久久久精品妇女99| 亚洲AⅤ优女AV综合久久久| 青青青青久久精品国产h久久精品五福影院1421 | 婷婷伊人久久大香线蕉AV | 日韩一区二区久久久久久| 久久久久亚洲AV成人片| a高清免费毛片久久| 久久香蕉国产线看观看99| 99久久国产综合精品成人影院| 久久免费高清视频|