无法编译C++程序

Can't compile C++ program

本文关键字:程序 C++ 编译      更新时间:2023-10-16

代码如下....

#include <iostream>
int main()
{
    cout << "WELCOME TO C++ PROGRAMMING";
    return 0;
}

当我进入终端并传递命令…

g++ hello.cpp

显示……

hello.cpp: In function ‘int main()’:
hello.cpp:4:2: error: ‘cout’ was not declared in this scope
  cout << "WELCOME TO C++ PROGRAMMING";
  ^
hello.cpp:4:2: note: suggested alternative:
In file included from hello.cpp:1:0:
/usr/include/c++/4.8/iostream:61:18: note:   ‘std::cout’
   extern ostream cout;  /// Linked to standard output

那么原因是什么呢?我该怎么办呢?

coutstd命名空间中找到。

#include <iostream>
int main()
{
    std::cout << "Welcome";
    return 0;
}

为了避免名称冲突,c++库包含在命名空间中,命名为std。因此,要使程序编译,要么添加:

using namespace std;

在程序的顶部,或者你用std:::

作为标准库的每个"对象"的前缀:
std::cout << "Welcome";

您应该在cout

之前使用std

你应该:

std::cout << "Welcome";