无法编译C ++程序

时间:2015-05-26 21:56:25

标签: c++ linux ubuntu terminal

这是代码....

#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

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

4 个答案:

答案 0 :(得分:8)

std命名空间中找到了

cout

#include <iostream>

int main()
{
    std::cout << "Welcome";
    return 0;
}

答案 1 :(得分:4)

为避免名称冲突,C ++库包含在名为std名称空间中。因此,要编译程序,可以添加:

using namespace std;

在您的程序顶部,您可以使用std::为标准库的每个“对象”添加前缀:

std::cout << "Welcome";

答案 2 :(得分:1)

你应该在cout之前使用std

答案 3 :(得分:0)

你应该:

std::cout << "Welcome";
相关问题