所以,我有这个问题.为什么cout会抛出
error C2065: 'cout' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)
我使用Visual Studio 2012作为IDE,我正在写一个学校项目.除了示例文件,我已经完成了所有工作.所以我试图在屏幕上写一些像这样的东西:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以问题是cout ... printf工作正常,但我想使用cout.
编辑:我已经改变了""到<>但它没有帮助.另外我仅使用此代码示例...这不是整个项目.
Mat*_*get 16
stdafx.h 应该是源文件中的第一个include指令.
切换文件并将第二个包含转换<>为其他建议.
#include "stdafx.h"
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅此帖子.
Mar*_*sen 11
首先:
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
代替 #include "iostream"
其次,写作通常被认为是不好的做法using namespace std;,尽管大多数课程都是从那开始的.在你的情况下,最好只使用你真正需要的东西:
using std::cout;
#include "iostream"
Run Code Online (Sandbox Code Playgroud)
应该
#include <iostream>
Run Code Online (Sandbox Code Playgroud)
引用这篇文章:difference- Between-iostream-and-iostream-quotes-in-include
感谢 @Jerry Coffin 的回答:
当您使用 < > 时,编译器仅在系统指定的目录(例如,您在 include 环境变量中设置的任何目录)中查找标头。
当您使用“”时,编译器首先在本地目录中查找,如果失败,则像您使用<>一样重新搜索。从技术上讲,(即根据标准)不一定是“本地”目录,但这就是它在我所知道的基本上每个编译器中的工作方式)。
编辑:
然而,根本原因是stdafx.h预编译头。Visual C++ 不会编译#include "stdafx.h"源文件中 之前的任何内容,除非/Yu'stdafx.h'未选中编译选项(默认情况下);它假设源代码中直到并包括该行的所有代码都已编译。然而,最好还是使用<>withiostream以免混淆代码的读者。