#include< iostream>,预编译头文件错误 [英] #include <iostream>, Precompiled headers error

查看:121
本文介绍了#include< iostream>,预编译头文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我最近购买了Sam''s Teach Yourself C ++ Fifth Edition。 (大约在第6版出版前一周)


我正在努力完成这本书,但在尝试编译Hello World时,我不断收到错误而不是它制作节目。


我出于某种原因环顾互联网,但是卡住了。


我唯一的假设是第五版升级到适合新的编译器? (我从Microsoft下载了Visual C ++ 2008 Express Edition。


我在Win32控制台项目中,输入如下代码:


#include< iostream>


int main()

{

std :: cout<<" Hello世界!\ n" ;;

返回0;

}


但是当我尝试编译时,我得到以下内容:


1> ------构建开始:项目:HelloWorld,配置:调试Win32 ------

1>编译。 ..

1> HelloWorld.cpp

1> c:\documents and settings\adams\my documents\visual studio 2008\projects\helloworld \ helloworld \ helloworld.cpp(1):警告C4627:''#include< iostream>'':在查找预编译头文件时跳过

1>添加指令到''stdafx。 h''或重建预编译的标题

1> c:\documents and settings \ adams \ my documents而寻找预编译头意外的文件结束:\\visual工作室2008\projects\helloworld\helloworld\helloworld.cpp(8):致命错误C1010。您是否忘记在源代码中添加''#include" stdafx.h"''?
1>构建日志保存在" file:// c:\Documents和Settings \\ \\ Adams\Mh Documents\Visual Studio 2008 \Projects\HelloWorld \HelloWorld \Debug \BuildLog .htm"

1> HelloWorld - 1个错误,1个警告( s)


对这个问题感到抱歉,只是想知道这是不是我做错了,或者我是否需要一本新书,或者在整本书中改变一些东西?

Hi there guys, I''ve recently purchased "Sam''s Teach Yourself C++ Fifth Edition" (About a week before the 6th edition came out)

I''m trying to work through the book, but in trying to compile Hello World, I keep getting an error instead of it making the program.

I''ve looked around the internet for a reason, but come up stuck.

My only assumption would be that the fifth edition is upgraded to suit newer compilers? (I downloaded Visual C++ 2008 Express Edition from Microsoft.

I am in a Win32 console project, and enter the code as follows:

#include <iostream>

int main()
{
std::cout << "Hello World!\n";
return 0;
}

But when I try and compile, I get the following:

1>------ Build started: Project: HelloWorld, Configuration: Debug Win32 ------
1>Compiling...
1>HelloWorld.cpp
1>c:\documents and settings\adams\my documents\visual studio 2008\projects\helloworld\helloworld\helloworld.cpp (1) : warning C4627: ''#include <iostream>'': skipped when looking for precompiled header use
1> Add directive to ''stdafx.h'' or rebuild precompiled header
1>c:\documents and settings\adams\my documents\visual studio 2008\projects\helloworld\helloworld\helloworld.cpp (8) : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ''#include "stdafx.h"'' to your source?
1>Build log was saved at "file://c:\Documents and Settings\Adams\My Documents\Visual Studio 2008\Projects\HelloWorld\HelloWorld\Debug\BuildLog .htm"
1>HelloWorld - 1 error(s), 1 warning(s)

Sorry about the question, just wonderinfg if it''s something I''m doing wrong, or if I need a new book, or to change something throughout the book?

推荐答案


对这个问题感到抱歉,只是想知道是不是我做错了,或者我是需要一本新书,还是要改变整本书中的内容?
Sorry about the question, just wonderinfg if it?s something I?m doing wrong, or if I need a new book, or to change something throughout the book?



您正在编译的代码是正确的。问题实际上与您的代码有点相关。有一些叫做预编译头的东西,你的IDE项目被配置为使用它。我现在不打算详细说明,因为它是你需要远离的东西。


创建一个新的C ++项目。在选择要制作的项目类型时,请确保选择以下选项。它必须是控制台项目。预编译头还有一个选项(可能默认选中)。确保未编译的预编译标题。创建这个新项目,尝试在那里编译相同的代码,并告诉我们它是怎么回事。

The code you are compiling is correct. The issue is actually a bit tangential to your code. There?s something called a precompiled header, and your IDE project is configured to use it. I won?t go into details now because it?s something you need to stay away from.

Create a new C++ project. Make sure you select the following options when selecting what kind of project to make. It must be a console project. There is also an option (probably selected by default) for precompiled headers. Make sure precompiled headers if not selected. Create this new project, try to compile the same code there, and tell us how it goes.


谢谢oler1s。


我创建了一个新的解决方案并关闭预编译头,再次在代码中输入并运行。虽然只是非常短暂地闪过哈哈。 (如果发生这种情况,我已经告诉我要添加两行来等待字符输入。)


所以我会假设本书中的每个程序,我不应该使用预编译头文件吗?
Thanks oler1s.

I created a new Solution and turned off the Precompiled headers, entered in the code again and it ran. Though flashed only very briefly haha. (The book I have has told me to add two more lines to wait for a character input if this happens.)

So I''ll assume for each program in this book, I shouldn''t be using Precompiled headers?


在了解预编译头文件以及如何使用它们之前,可能不是一个错误的假设。


这是VC ++ 2008与您预订示例之间的互操作性问题。


本书示例假设示例中的代码是唯一的代码,足够合理。但是,如果在VC ++ 2008中启用预编译头,则假定您通过头调用stdafx.h创建预编译头,即使可以使用任何头。


因为stdafx.h不是在本书的例子中你会收到一个错误。


无论如何,保持预编译的标题关闭,这些书中的任何一个例子都不大,它们对编译时间没有任何影响。预编译头是一种加速大型项目编译的方法。


如果你的程序快速闪过那么另一点你可能按F5运行它,那就是在调试器中运行它。这没有正确运行它并且调试器假定如果你想让它停止你将设置一个断点。


尝试改为< Ctrl> -F5这告诉IDE正常执行程序,它应该在执行结束时停止,按任意键继续。 。 "消息。


你不应该添加代码行来暂停执行任何程序这是非常糟糕的做法,不需要,因为如果你使用它,系统会为你做正确地阻止你的程序被另一个程序运行。
Probably not a bad assumption until you understand what precompiled headers are and how to use them.

This is an interoperability problem between VC++ 2008 and you book examples.

The book examples assume that the code in the example is the only code, reasonably enough. However if you enable precompiled headers in VC++ 2008 it assumes you are creating your precompiled headers through a header call stdafx.h even though any header could be used.

Since stdafx.h is not in the book examples you get an error.

Anyway, keep precompiled headers switch off, it is unlikely any of the book examples will be big enough that they would make any difference to compilation time. Precompiled headers are a way of speeding up compilation on a large project.


On another point if your program flashed through quickly then you probably ran it by pressing F5, that is ran it in the debugger. This is not running it properly and the debugger assumes that if you want it to stop you will have set a break-point.

Try instead <Ctrl>-F5 this tells the IDE to execute the program normally and it should stop at the end of execution with a "Press any key to continue . . ." message.

You should not be adding lines of code to pause any program when its execution has finished this is extremely poor practice, not required because the system does it for you if you use it properly and prevents your program from being run by another program.


这篇关于#include&lt; iostream&gt;,预编译头文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆