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

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

下面是我用 nano 编辑器编写的 hello world 程序。

#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;
}