簡體   English   中英

使用 c++ 編譯簡單程序的問題

[英]problem with compiling a simple program with c++

我對 C++ 真的很陌生。 我正在嘗試編譯這個簡單的程序,但出現錯誤

#include <iostream> 
int main(int argc, char** argv) 
{ 
   cout << "You have entered " << argc 
        << " arguments:" << "\n"; 
 
   for (int i = 0; i < argc; ++i) 
       cout << argv[i] << "\n"; 
 
   return 0; 
} 

我得到的錯誤是

(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

我嘗試將其編譯為 g++ file1.cpp 我也嘗試過 g++ -c file1.cpp 然后 Z644B1F5211D64EC52727780E741.o.o.exe 文件不起作用我究竟做錯了什么?

如果您想要 main.cpp 之類的東西,可以嘗試在一個空目錄名稱中創建一個新文件,並將代碼放入命名空間和其他所有內容所需的代碼中並運行

#include <iostream>
using namespace std;
    int main(int argc, char** argv) 
        { 
           cout << "You have entered " << argc 
                << " arguments:" << "\n"; 
         
           for (int i = 0; i < argc; ++i) 
               cout << argv[i] << "\n"; 
         
           return 0; 
        } 

它應該在 output 結果之后工作也許查看這篇媒體文章以了解更多關於為什么需要使用命名空間 std https://medium.com/breaktheloop/why-using-namespace-std-is-used-after-include-iostream -dc5ae45db652

您可以嘗試像這樣指定命名空間->

#include <iostream>
using namespace std;

編輯“必須”替換為“可以嘗試”。 它有效,但為什么!

暫無
暫無

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

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