Visual Studio 2012 c++ hello world - iostream 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12614753/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:25:11  来源:igfitidea点击:

visual studio 2012 c++ hello world - iostream not working

c++visual-studiovisual-c++

提问by Nik

I have an issue with Visual Studio's 2012. I am also using "Sams Teach Yourself C++ in One Hour a day, 7th edition".

我的 Visual Studio 2012 有问题。我也在使用“Sams Teach Yourself C++ in One Hour a day, 7th edition”。

After using google to find the "best" compilers for C++, Visual Studios seemed to be the tool of choice.

在使用 google 找到 C++ 的“最佳”编译器后,Visual Studios 似乎是首选工具。

So I downloaded and installed Visual Studios 2012. The very first lesson in the book is (and tells me to run it as a console app by going to File > New > Project >Visual C++ > Win32 > Console Application)

所以我下载并安装了 Visual Studios 2012。这本书的第一课是(并告诉我通过转到File > New > Project >Visual C++ > Win32 > Console Application将它作为控制台应用程序运行)

 #include <iostream>

int main()
{
 std::cout << “Hello World!” << std::endl;
 return 0;
}

which doesnt work, at all. it outputs an error message similiar to the following:

这根本不起作用。它输出类似于以下内容的错误消息:

1>c:\users\nik\documents\visual studio 2012\projects\consoleapplication4\consoleapplication4\consoleapplication4.cpp(8): error C2065: '“Hello' : undeclared identifier 1> Generating Code... ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

1>c:\users\nik\documents\visual studio 2012\projects\consoleapplication4\consoleapplication4\consoleapplication4.cpp(8): error C2065: '“Hello': undeclared identifier 1> Generating Code... ====== ====== 构建:0 成功,1 失败,0 最新,0 跳过 ==========”

(there is more lines similiar to the first, but its rather long)

(与第一行类似的行数更多,但相当长)

However, after googling and watching a video on youtube the following code works (using File > New > Project >Visual C++ > General > Empty Project)

但是,在谷歌搜索并在 youtube 上观看视频后,以下代码有效(使用File > New > Project >Visual C++ > General > Empty Project

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

using namespace std;

int main() {
cout << "Hello Nik" << endl;
_getch();

return 0;
}

Does Visual Studio's 2012 have a C++ compiler? or does it just have a visual c++ compiler (if thats even the issue, only reason I think it could be is I can see templates for Visual C++ but none for c++ by itself...) or do I need to download Visual Studio Express to download native c++ ??

Visual Studio 2012 有 C++ 编译器吗?或者它是否只有一个 Visual C++ 编译器(如果这就是问题所在,我认为可能的唯一原因是我可以看到 Visual C++ 的模板,但没有 C++ 本身的模板......)或者我是否需要下载 Visual Studio Express下载本机 C++ ??

Any help would be greatly appreciated as I am feeling some-what out of my depth here...

任何帮助将不胜感激,因为我在这里感觉有些超出我的深度......

Thanks.

谢谢。

回答by Kamil

Besides aphostrophes you may need to disable precompiler headers in project properties.

除了 aphostrophes您可能需要在项目属性中禁用预编译头。

They are turned on by default in VS2012. If you are not familiar with precompiled headers turn them off.

它们在 VS2012 中默认开启。如果您不熟悉预编译头文件,请将它们关闭。

  1. Right click on project (not solution)
  2. Click properties.
  3. Expand "Configuration properties"
  4. Expand "C/C++"
  5. Choose "Precompiled headers"
  6. Set "Precompiled header" to "Not Using Precompiled Headers"
  1. 右键单击项目(不是解决方案)
  2. 单击属性。
  3. 展开“配置属性”
  4. 展开“C/C++”
  5. 选择“预编译头”
  6. 将“预编译头”设置为“不使用预编译头”

More information about precompiled headers and stdafx.h file at Wikipedia

维基百科上有关预编译头文件和 stdafx.h 文件的更多信息

回答by Alex R.

The apostrophes you used are wrong:

您使用的撇号是错误的:

“Hello World!” 

should be

应该

"Hello World!"

Notice even how SO recognizes the difference. You should at least type the code you see in the book instead of copying and pasting it. ;-)

甚至注意 SO 如何识别差异。你至少应该输入你在书中看到的代码,而不是复制和粘贴它。;-)

回答by Konstantin Dinev

The Win32 console application is actually quite different from the empty project. Win32 utilize a message (input) queue which you poll in a loop and your program respectively utilizes the Win32 API and performs certain operations.

Win32 控制台应用程序实际上与空项目有很大不同。Win32 使用您在循环中轮询的消息(输入)队列,您的程序分别使用 Win32 API 并执行某些操作。

The empty project is a bit less dependent on Win32 or anything that Windows provides in terms of API unless you make it dependent on it. This would be the simples hello world app in you empty project:

空项目不太依赖 Win32 或 Windows 在 API 方面提供的任何东西,除非你让它依赖它。这将是您空项目中的简单 hello world 应用程序:

#include <iostream>

using namespace std;

int main() 
{
    cout << "Hello World" << endl;

    return 0;
}

回答by Abhineet

Just try this::

试试这个::

"Hello World!"instead of “Hello World!”.

“你好,世界!” 而不是“你好世界!” .

回答by guest

In order to fix your error you have to delete std:: of std::cout and std::endl, and write using namespace std; underneath #include iostream and and change “ ” with " ".

为了修复您的错误,您必须删除 std::cout 和 std::endl 的 std::,并使用命名空间 std; 编写;在#include iostream 下方,并将“ ” 更改为“ ”。

#include <iostream>
using namespace std;
int main() 
{
   cout <<"Hello World" << endl;
   return 0;
}

In Visual studio 2012

在 Visual Studio 2012 中

 file>new projet>visual c++ (Project win32)>application settings(application console+Not Using Precompiled)>in right box in you Project (right click, add>new element>file c++).

回答by guest

The difference between

和...之间的不同

“Hello World!” and 
"Hello Nik" is the apostrophe. 

Aslo is the error persists than just check visual c++ library linker.

Aslo 是错误仍然存​​在,而不仅仅是检查 Visual C++ 库链接器。

There is aslo definitely no need for conio.h

绝对不需要 conio.h

If your going to copy from a book at least copy it correctly.

如果您要从书中复制,至少要正确复制。

Using namespace std; 
would be pretty smart in this case.