Code :: Blocks/Dev-c ++:错误:iostream:没有这样的文件或目录

sap*_*sap 2 c++ dev-c++

我从这里下载了Code :: Blocks:http://www.codeblocks.org/downloads/26

我正在学习c编程.当我运行以下程序时,我收到错误:

iostream: No such file or directory
error: syntax error before "namespace"
warning: type defaults to `int' in declaration of `std'
warning: data definition has no type or storage class
In function `main':
error: `cout' undeclared (first use in this function)
error: (Each undeclared identifier is reported only once
error: for each function it appears in.)
error: `cin' undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)

我正在运行以下程序:

#include <iostream>
using namespace std;

int main()
{
  int x;

  x = 0;
  do {
    // "Hello, world!" is printed at least one time
    //  even though the condition is false
    cout<<"Hello, world!\n";
  } while ( x != 0 );
  cin.get();
}
Run Code Online (Sandbox Code Playgroud)

我尝试了Dev-C++,我得到了同样的错误.如何解决这个问题?

谢谢,莎拉

Kev*_*son 9

这是在"program.c"或"program.cpp"这样的文件中吗?如果它是.c文件,那么您的编译器可能会将其解释为C,而不是C++.这很容易导致这样的错误.可以"强制"编译器将此类扩展视为另一个,但默认情况下,.c文件用于C,而.cpp文件编译为C++.

它要么是这个,要么以某种方式标准库的默认"包含"目录没有正确设置,但我不知道你是如何修复它的,因为它依赖于编译器/环境.


Ole*_*xiy 6

我在尝试在 Code::Blocks 中运行我的第一个程序时也遇到了这个问题。我的文件以“.c”扩展名保存为“test.c”,当我将其保存为“test.cpp”时,它运行良好。

还值得一提的是,在成功编译新的“test.cpp”文件之前,我必须重新启动 Code::Blocks


小智 -4

尝试包含iostream.h而不是iostream.

  • 不,不要这样做。`iostream.h` 太旧了。`&lt;iostream&gt;` 是您应该为 C++ 代码包含的内容。 (3认同)