代码::块/ Dev-c++:错误:iostream:没有这样的文件或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10270780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code::Blocks/ Dev-c++: error: iostream: No such file or directory
提问by sap
I downloaded Code::Blocks from here: http://www.codeblocks.org/downloads/26
我从这里下载了 Code::Blocks:http: //www.codeblocks.org/downloads/26
I'm learning c programming. When I run the following program, I get error:
我正在学习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)
I'm running the following program:
我正在运行以下程序:
#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();
}
I tried Dev-C++, I get the same error. How to fix this?
我试过 Dev-C++,我得到了同样的错误。如何解决这个问题?
Thanks, Sarah
谢谢,莎拉
回答by Kevin Anderson
Is this in a file like "program.c" or "program.cpp"? If it's a .c file, then your compiler may be interpreting it as C, and not C++. This could easily cause such an error. It's possible to "force" the compiler to treat either such extension as the other, but by default, .c files are for C, and .cpp files are compiled as C++.
这是在“program.c”或“program.cpp”之类的文件中吗?如果它是一个 .c 文件,那么您的编译器可能会将它解释为 C,而不是 C++。这很容易导致这样的错误。可以“强制”编译器将这样的扩展名视为另一个,但默认情况下,.c 文件用于 C,而 .cpp 文件编译为 C++。
It's either this, or somehow your default "include" directories for the standard library are not set up right, but I don't know how you'd fix that, as that'd be compiler/environment dependent.
要么是这个,要么是您的标准库的默认“包含”目录设置不正确,但我不知道您将如何解决该问题,因为这取决于编译器/环境。
回答by Olexiy
I also had that problem when trying to run my first program in Code::Blocks. My file was saved with '.c' extension as 'test.c' and when I saved it as 'test.cpp', it worked fine.
我在尝试在 Code::Blocks 中运行我的第一个程序时也遇到了这个问题。我的文件以“.c”扩展名保存为“test.c”,当我将其保存为“test.cpp”时,它运行良好。
It is also worth mentioning that I had to restart Code::Blocks before new 'test.cpp' file was compiled successfully.
还值得一提的是,在成功编译新的“test.cpp”文件之前,我必须重新启动 Code::Blocks。
回答by jibranejaz
While saving your source code before compiling just save the name with extension ".cpp". You wont get the error..
在编译之前保存源代码时,只需保存扩展名为“.cpp”的名称。你不会得到错误..
回答by Aseem Ahir
I got the same problem.
我遇到了同样的问题。
Change #include < iostream.h > to #incude < iostream >
将 #include <iostream.h> 更改为 #incude <iostream>
Consequently, in your program, change every keyword related to iostream, such as cin cout and endl to std::cout, std::cin and std::endl
因此,在您的程序中,将与 iostream 相关的每个关键字(例如 cin cout 和 endl)更改为 std::cout、std::cin 和 std::endl
That'll do the trick
这样就行了
回答by Javed Iqbal
Use <iostream>
instead of <iostream.h>
and add std::
before cout
, cin
etc
使用<iostream>
替代<iostream.h>
和补充std::
之前cout
,cin
等
Use std::cout << "Welcome";
instead of cout << "Welcome";
使用 std::cout << "Welcome";
代替 cout << "Welcome";
Save the file with .cpp
extension
用.cpp
扩展名保存文件
回答by Ken Cole
Apparently you want to create a c++ file. But you allowed your computer to auto provide the file extension C/C++. When it does that it automatically provides a file extension of ".c". Which is not corect. You want ".cpp".
显然你想创建一个 C++ 文件。但是您允许您的计算机自动提供文件扩展名 C/C++。当它这样做时,它会自动提供“.c”的文件扩展名。这是不正确的。你想要“.cpp”。
Solution: Rename your file with a ".cpp" extension, or else explicitly state your extension when saving new files by putting ".cpp" (without quotes of course) after your intended file name; i.e. specify your file extension.
解决方案:使用“.cpp”扩展名重命名您的文件,或者在保存新文件时通过在您想要的文件名后放置“.cpp”(当然不带引号)来明确声明您的扩展名;即指定您的文件扩展名。
回答by I.T.S Channel
I tried in Dev-C++ . Instead of iostream.h use iostream also write the using namespace std;
我在 Dev-C++ 中尝试过。代替 iostream.h 使用 iostream 也编写 using 命名空间 std;
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
return 0;
}
回答by Meghraj Sharma
you have missing iostream.h file in you mingw directory folder placed inside codeblocks/devc++. what you have to do is just download the file from link given below and replace with your previous mingw folder in codeblocks/devc++.
您在放置在 codeblocks/devc++ 中的 mingw 目录文件夹中缺少 iostream.h 文件。您所要做的就是从下面给出的链接下载文件,并替换为您之前在 codeblocks/devc++ 中的 mingw 文件夹。
回答by Hughie
I found the problem was cause by having a previous version of cgg and cpp in a Perl installation. The Perl structure did not have the correct library files. When I added C:\MinGW\bin
and C:\MinGW\MSYS\1.0\bin
to the path, I added them at the end so it picked up the Perl install first. I moved the path variable entries to the beginning and reopened my cmd window and it now works because it finds the MinGW version first.
我发现问题是由于在 Perl 安装中使用了以前版本的 cgg 和 cpp 引起的。Perl 结构没有正确的库文件。当我将C:\MinGW\bin
和添加C:\MinGW\MSYS\1.0\bin
到路径时,我在最后添加了它们,因此它首先选择了 Perl 安装。我将路径变量条目移到开头并重新打开我的 cmd 窗口,它现在可以工作,因为它首先找到了 MinGW 版本。
Type path to see your path environment varialble. Mine now looks like:
键入 path 以查看您的路径环境变量。我的现在看起来像:
C:\MinGW>path
PATH=C:\MinGW\bin;C:\MinGW\MSYS.0\bin;C:\Perl\site\bin;C:\Perl\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\WIDCOMM\BluetoothSoftware\;
回答by user3023906
you written your program in C++ code use c code then your program run correctly
你用 C++ 代码编写你的程序使用 c 代码然后你的程序正确运行
in first line use it
在第一行使用它
#include <Io stream.h>
main ()
{
in ending line use it
在结束行使用它
system (pause");