在g++ubuntu中编译简单的helloworldc++程序时出现多个错误

multiple errors while compiling simple hello world c++ program in g++ ubuntu

本文关键字:错误 程序 helloworldc++ g++ubuntu 编译 简单      更新时间:2023-10-16

下面是我在nano编辑器中编写的helloworld程序。

#include<iostream>
using namespace std;
int main
 {
   cout<< "Hello world";
   return 0;
 }

当我编译它的时候,我得到了这么多错误。

ello.cpp:4:5: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
 int main
     ^
hello.cpp:6:22: error: expected ‘}’ before ‘;’ token
  cout<< "Hello world";
                      ^
hello.cpp:6:6: error: invalid user-defined conversion from ‘std::basic_ostream<char>’ to ‘int’ [-fpermissive]
  cout<< "Hello world";
      ^
In file included from /usr/include/c++/4.8/ios:44:0,
                 from /usr/include/c++/4.8/ostream:38,
                 from /usr/include/c++/4.8/iostream:39,
                 from hello.cpp:1:
/usr/include/c++/4.8/bits/basic_ios.h:115:7: note: candidate is: std::basic_ios<_CharT, _Traits>::operator void*() const [with _CharT = char; _Traits = std::char_traits<char>] <near match>
       operator void*() const
       ^
/usr/include/c++/4.8/bits/basic_ios.h:115:7: note:   no known conversion for implicit ‘this’ parameter from ‘void*’ to ‘int’
hello.cpp:7:2: error: expected unqualified-id before ‘return’
  return 0;
  ^
hello.cpp:8:2: error: expected declaration before ‘}’ token
  }

请帮帮我。

您错过了将main()标记为函数的括号:

#include <iostream>
int main() {
    std::cout << "!!!Hello World!!!" << std::endl;
    return 0;
}

请注意,如果显式使用std::coutstd::endl而不是using整个namespace std(如果您不清楚发生了什么,这可能会让您感到困惑),则更容易理解名称空间。或者,至少要清楚你是什么using:

#include <iostream>
using std::cout;
using std::endl;
int main() {
    cout << "!!!Hello World!!!" << endl;
    return 0;
}