简体   繁体   English

Visual C++ 无法打开包含文件“iostream”

[英]Visual C++ can't open include file 'iostream'

I am new to C++.我是 C++ 新手。 I just started!我刚开始! I tried a code on Visual C++ 2010 Express version, but I got the following code error message.我在 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 ==========

This is the code:这是代码:

// 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;
}

Replace代替

#include <iostream.h>

with

using namespace std;

#include <iostream>

Some things that you should check:您应该检查的一些事项:

  • Check the include folder in your version of Visual Studio ( in "C:\Program Files\Microsoft Visual Studio xx.x\VC\include", check for the file which you are including, iostream , make sure it's there ).检查您的 Visual Studio 版本中的包含文件夹(在“C:\Program Files\Microsoft Visual Studio xx.x\VC\include”中,检查您包含的文件iostream ,确保它在那里)。

  • Check your projects Include Directories in <Project Name> → PropertiesConfiguration PropertiesVC++ DirectoriesInclude Directories (it should look like this: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include; )检查您的项目在 <Project Name> → PropertiesConfiguration PropertiesVC++ DirectoriesInclude Directories中包含目录(它应该如下所示: $(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSdkDir)include;$(FrameworkSDKDir)\include; )

  • Make sure that you selected the correct project for this code (menu FileNewProjectVisual C++Win32 Console Application )确保您为此代码选择了正确的项目(菜单FileNewProjectVisual C++Win32 Console Application

  • Make sure that you don't have <iostream.h> anywhere in your code files, Visual Studio doesn't support that ( in the same project, check your other code files, .cpp and .h files for <iostream.h> and remove it ).确保您的代码文件中没有<iostream.h> ,Visual Studio 不支持(在同一个项目中,检查您的其他代码文件、.cpp 和 .h 文件中的<iostream.h>并删除它)。

  • Make sure that you don't have more than one main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the* main()` function and remove it or replace it with another name ).确保main() function in your project code files (*in the same project, check your other code files, .cpp and .h files for the* main()`函数并将其删除或用另一个名称替换它)。

Some things you could try building with:您可以尝试构建的一些东西:

  • Exclude using namespace std;排除using namespace std; from your main() function and put it after the include directive.从您的main()函数中,并将其放在 include 指令之后。
  • Use std::cout without using namespace std;使用std::cout而不using namespace std; . .

I had this exact same problem in Visual Studio 2015 .我在Visual Studio 2015中遇到了同样的问题。 It looks like as of Visual Studio 2010 and later you need to include #include "stdafx.h" in all your projects.它看起来像Visual Studio 2010和更高版本,您需要在所有项目中包含#include "stdafx.h"

#include "stdafx.h"
#include <iostream>
using namespace std;

The above worked for me.以上对我有用。 The below did not:以下没有:

#include <iostream>
using namespace std;

This also failed:这也失败了:

#include <iostream>
using namespace std;
#include "stdafx.h"

You are more than likely missing $(IncludePath) within PropertiesVC++ DirectoriesInclude Directories .您很可能在PropertiesVC++ DirectoriesInclude Directories中缺少$(IncludePath)

Adding this should make iostream and others visible again.添加它应该使 iostream 和其他人再次可见。 You probably deleted it by mistake while setting up your program.您可能在设置程序时错误地删除了它。

It is possible that your compiler and the resources installed around it were somehow incomplete.您的编译器和围绕它安装的资源可能不完整。 I recommend re-installing your compiler: it should work after that.我建议重新安装你的编译器:它应该在那之后工作。

I got this error when I created an 'Empty' console application in Visual Studio 2015 .Visual Studio 2015中创建“空”控制台应用程序时出现此错误。 I recreated the application, leaving the 'Empty' box unchecked.我重新创建了应用程序,未选中“空”框。 It added all of the necessary libraries.它添加了所有必要的库。

If your include directories are referenced correctly in the VC++ project property sheet → Configuration PropertiesVC++ directoriesInclude directories , the path is referenced in the macro $(VC_IncludePath) .如果在 VC++ 项目属性表 →配置属性VC++ 目录包含目录中正确引用了您的包含目录,则在宏$(VC_IncludePath)中引用了路径。

In my Visual Studio 2015 this evaluates to: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include"在我的 Visual Studio 2015 中,计算结果为:“C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include”

using namespace std;
#include <iostream>

That did it for me.那是为我做的。

Microsoft Visual Studio is funny. Microsoft Visual Studio 很有趣。 When you're using the installer, you must checkbox a lot of options to bypass the .NET framework (somewhat) to make more C++ instead of C# applications, such as the CLR options under desktop development... in the Visual Studio installer.... the difference is the C++ Win32 console project or a C++ CLR console project.当您使用安装程序时,您必须选中许多选项以绕过 .NET 框架(在某种程度上)以制作更多 C++ 而不是C#应用程序,例如 Visual Studio 安装程序中桌面开发下的 CLR 选项。 ...区别在于 C++ Win32 控制台项目或 C++ CLR 控制台项目。

So what's the difference?那么有什么区别呢? Well, I'm not going to list all of the files CLR includes, but since most good C++ kernels are in Linux... So CLR allows you to bypass a lot of the Windows .NET framework because Visual Studio was really meant for you to make applications in C#.好吧,我不会列出 CLR 包含的所有文件,但由于大多数优秀的 C++ 内核都在 Linux 中......所以 CLR 允许您绕过很多 Windows .NET 框架,因为 Visual Studio 真的适合您在 C# 中制作应用程序。

Here's a C++ Win32 console project!这是一个 C++ Win32 控制台项目!

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello, World!" << endl;
    return 0;
}

Now here's a C++ CLR console project!现在这是一个 C++ CLR 控制台项目!

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine("Hello, World!");
    return 0;
}

Both programs do the same thing .... the CLR just looks more frameworked class overloading methodology, so Microsoft can great its own vast library you should familiarize yourself with if so inclined.两个程序都做同样的事情.... CLR 只是看起来更框架化的类重载方法,因此微软可以完善自己的庞大库,如果愿意的话,您应该熟悉自己。

Keywords (C++)关键字 (C++)

Other things you'll learn from debugging to add for error avoidance:您将从调试中学到的其他内容以添加以避免错误:

#ifdef _MRC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

Make sure you have Desktop Development with C++ installed.确保您安装了带有 C++ 的桌面开发

I was experiencing the same problem, because I only had Universal Windows Platform Development installed.我遇到了同样的问题,因为我只安装了通用 Windows 平台开发

Quick fix for small programs:小程序的快速修复:

Add: #include <cstdlib>添加: #include <cstdlib>

In my case, my Visual Studio 2015 installed without selecting C++ package , and Visual Studio 2017 is installed with the C++ package .在我的情况下,我的Visual Studio 2015安装时没有选择C++ package ,而Visual Studio 2017是使用C++ package安装的。 If I use Visual Studio 2015, opening a C++ project will show this error, and using Visual Studio 2017 will be no error.如果我使用Visual Studio 2015,打开一个C++项目会出现这个错误,而使用Visual Studio 2017则不会出现错误。

If you created an environment variable with the name IncludePath , try renaming it to something else.如果您创建了一个名为IncludePath的环境变量,请尝试将其重命名为其他名称。

This name will override $(IncludePath) inside project properties.此名称将覆盖项目属性中的$(IncludePath)

I had this problem too.我也有这个问题。 I used this code (before main(); ) in Visual Studio 2022 , and it turned OK:我在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM