簡體   English   中英

從終端在 linux 機器上用 C++ 制作和包含頭文件

[英]making and including header files in c++ on linux machine from terminal

首先,我對 C++ 和 Linux 還很陌生,所以如果你能在回復時記住這一點,那就太好了:)

我正在嘗試在一個文件中創建一個類,當我創建該類的實例時,我將在另一個文件的標題中實現該類。

我的類文件是

#include <iostream>

int main(){

class Box 
{
public:

int _width;
int _length;
int _height;

};
}

我將它保存為 boxclass.h,但我沒有編譯它。 我在某處讀到當我將它添加到我的頭文件時,我應該將它保存為文本文件。

我試圖在其中包含我的 box 類的另一個文件是:

#include <iostream>
#include "/home/cole/cpp/boxclass.h"

using namespace std;


int main()

{

Box outer{3,4,5};
Box inner{1,2,3};

Box newB = outer-inner;

cout << newB << endl;

}

當我嘗試編譯它時,這些錯誤會以許多不同的值重復多次

/home/cole/cpp/Boxclass.h:442:864: warning: null character(s) ignored
/home/cole/cpp/Boxclass.h:442:1: error: stray ‘\1’ in program

任何人都可以向我解釋發生了什么事嗎?

main() {}函數有兩個定義。 這不符合任何 C++ 編譯代碼。

此外,您在此處有您班級的本地聲明:

int main(){

    class Box 
    {
    public:

        int _width;
        int _length;
        int _height;

    };
}

您不想要這個,而是出現在單獨的頭文件中的class Box的超出范圍的聲明。


我想你想要的是

#include <iostream>

class Box { 
public: 
   int _width;
   int _length;
   int _height;
};

int main(){

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM