[英]Visual C++ can't open include file 'iostream'
我是 C++ 新手。 我刚开始! 我在 Visual C++ 2010 Express 版本上尝试了代码,但收到以下代码错误消息。
------ Build started: Project: abc, Configuration: Debug Win32 ------ ugo.cpp c:\users\castle\documents\visual studio 2010\projects\abc\abc\ugo.cpp(3): fatal error C1083: Cannot open include file: 'iostream': No such file or directory ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这是代码:
// first.cpp -- displays a message
#include <iostream> // A PREPROCESSOR directive
int main(void) // Function header
{ // Start of a function body
using namespace std;
cout << "Come up and C++ me sometime.\n"; // Message
// Start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}
代替
#include <iostream.h>
和
using namespace std;
#include <iostream>
您应该检查的一些事项:
检查您的 Visual Studio 版本中的包含文件夹(在“C:\Program Files\Microsoft Visual Studio xx.x\VC\include”中,检查您包含的文件iostream
,确保它在那里)。
检查您的项目在 <Project Name> → Properties → Configuration Properties → VC++ Directories → Include Directories中包含目录(它应该如下所示: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include;
)
确保您为此代码选择了正确的项目(菜单File → New → Project → Visual C++ → Win32 Console Application )
确保您的代码文件中没有<iostream.h>
,Visual Studio 不支持(在同一个项目中,检查您的其他代码文件、.cpp 和 .h 文件中的<iostream.h>并删除它)。
确保main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the*
main()`函数并将其删除或用另一个名称替换它)。
您可以尝试构建的一些东西:
using namespace std;
从您的main()
函数中,并将其放在 include 指令之后。std::cout
而不using namespace std;
.我在Visual Studio 2015中遇到了同样的问题。 它看起来像Visual Studio 2010和更高版本,您需要在所有项目中包含#include "stdafx.h"
。
#include "stdafx.h"
#include <iostream>
using namespace std;
以上对我有用。 以下没有:
#include <iostream>
using namespace std;
这也失败了:
#include <iostream>
using namespace std;
#include "stdafx.h"
您很可能在Properties → VC++ Directories → Include Directories中缺少$(IncludePath) 。
添加它应该使 iostream 和其他人再次可见。 您可能在设置程序时错误地删除了它。
您的编译器和围绕它安装的资源可能不完整。 我建议重新安装你的编译器:它应该在那之后工作。
在Visual Studio 2015中创建“空”控制台应用程序时出现此错误。 我重新创建了应用程序,未选中“空”框。 它添加了所有必要的库。
如果在 VC++ 项目属性表 →配置属性→ VC++ 目录→包含目录中正确引用了您的包含目录,则在宏$(VC_IncludePath)中引用了路径。
在我的 Visual Studio 2015 中,计算结果为:“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include”
using namespace std;
#include <iostream>
那是为我做的。
Microsoft Visual Studio 很有趣。 当您使用安装程序时,您必须选中许多选项以绕过 .NET 框架(在某种程度上)以制作更多 C++ 而不是C#应用程序,例如 Visual Studio 安装程序中桌面开发下的 CLR 选项。 ...区别在于 C++ Win32 控制台项目或 C++ CLR 控制台项目。
那么有什么区别呢? 好吧,我不会列出 CLR 包含的所有文件,但由于大多数优秀的 C++ 内核都在 Linux 中......所以 CLR 允许您绕过很多 Windows .NET 框架,因为 Visual Studio 真的适合您在 C# 中制作应用程序。
这是一个 C++ Win32 控制台项目!
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" << endl;
return 0;
}
现在这是一个 C++ CLR 控制台项目!
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine("Hello, World!");
return 0;
}
两个程序都做同样的事情.... CLR 只是看起来更框架化的类重载方法,因此微软可以完善自己的庞大库,如果愿意的话,您应该熟悉自己。
您将从调试中学到的其他内容以添加以避免错误:
#ifdef _MRC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
确保您安装了带有 C++ 的桌面开发。
我遇到了同样的问题,因为我只安装了通用 Windows 平台开发。
小程序的快速修复:
添加: #include <cstdlib>
在我的情况下,我的Visual Studio 2015安装时没有选择C++ package ,而Visual Studio 2017是使用C++ package安装的。 如果我使用Visual Studio 2015,打开一个C++项目会出现这个错误,而使用Visual Studio 2017则不会出现错误。
如果您创建了一个名为IncludePath的环境变量,请尝试将其重命名为其他名称。
此名称将覆盖项目属性中的$(IncludePath) 。
我也有这个问题。 我在Visual Studio 2022中使用了这段代码(在main();之前),结果没问题:
#include "pch.h"
#include <iostream>
using namespace std;
using namespace winrt;
using namespace Windows::Foundation;
// first.cpp -- displays a message
#include <iostream> // a PREPROCESSOR directive
using namesapce std;
int main() // function header
{ // start of a function body
///using namespace std;
cout << "Come up and C++ me sometime.\n"; // message
// start a new line
cout << "Here is the total: 1000.00\n";
cout << "Here we go!\n";
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.