[英]How to compile a c++ program using Dev C++ compiler in windows command prompt?
[英]How to compile C++ program with a main, header and implementation file using command prompt, notepad and MinGW?
我正在嘗試自己學習 C++。 我已經在我的計算機中安裝了 minGW 並編譯 any.cpp 文件,我在命令提示符中使用命令 g++ -o main main.cpp 進行編譯,然后鍵入 main.exe 來運行它,它通常可以工作。 我用記事本寫.cpp文件。 但是,當我嘗試使用單獨的 header 文件(例如 test.h)和實現文件(例如 test.cpp)編譯程序時,它不會編譯。 我在命令提示符下輸入了代碼 g++ main.cpp test.cpp,但它顯示錯誤。 我不確定我在命令提示符中輸入的代碼是否正確,但我在這個 pdf 文件中找到了它。 鏈接到 pdf 文件
我准確地輸入了 pdf 文件中給出的第一個代碼。 為方便起見,代碼如下:
文件:Num.h
class Num{
private:
int num;
public:
Num(int n);
int getNum();
};
文件:Num.cpp
#include "Num.h"
Num::Num() : num(0) { }
Num::Num(int n): num(n) {}
int Num::getNum()
{
return num;
}
文件:main.cpp
#include <iostream>
#include "Num.h"
using namespace std;
int main()
{
Num n(35);
cout << n.getNum() << endl;
return 0;
}
並從我輸入的命令提示符編譯它
g++ main.cpp Num.cpp
但是 cmd 顯示錯誤消息“ no declaration matches 'Num::Num()'
”。
因為我是新手,所以我不知道該怎么做或這段代碼有什么問題。 任何幫助深表感謝。
您 header 不包括您的零參數構造函數。
嘗試
class Num{
private:
int num;
public:
Num(); //added this
Num(int n);
int getNum();
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.