简体   繁体   English

cin 和 cout 运算符错误 C++

[英]cin and cout operator error C++

I am doing an assignment for school and at school we use codeblocks for our IDE, but I wanted to use visual studio at home.我正在为学校做作业,在学校我们为 IDE 使用代码块,但我想在家里使用 Visual Studio。 The problem is when I run my program on visual studio I continue to get an operator error on my cin and cout but only before a string.问题是当我在 Visual Studio 上运行我的程序时,我的 cin 和 cout 继续出现操作员错误,但仅在字符串之前。 I tried #include and while the error lines went away when I build I get the same error before.我尝试了 #include 并且当我构建时错误行消失了,但我之前遇到了同样的错误。 I am new to C++ and rather confused any help is appreciated.我是 C++ 的新手,很困惑,任何帮助都值得赞赏。 picture of code below下面的代码图片

The #include <string> you tried would be correct.您尝试的#include <string>是正确的。

Your main problem is the #include "stdafx.h" .您的主要问题是#include "stdafx.h" It's a Precompiled header .这是一个预编译头文件 Visual studio puts it in by default although it isn't required. Visual Studio 默认情况下将其放入,尽管它不是必需的。 It has to be the first include if you use it:如果你使用它,它必须是第一个包含:

Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); Visual C++ 不会编译源文件中 #include "stdafx.h" 之前的任何内容,除非编译选项 /Yu'stdafx.h' 未选中(默认情况下); it assumes all code in the source up to and including that line is already compiled.它假定源代码中直到并包括该行的所有代码都已编译。

Just put it as the first #include (or get rid of it completely) to fix your issue.只需将其作为第一个 #include (或完全摆脱它)即可解决您的问题。 Then the rest should start working normally.然后其余的应该开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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