2012-02-15 118 views
0

我刚开始C++我正在阅读电子书从C++第7版开始。我复制了本书中的代码,并将其放入Visual,带有预编译头文件的新项目w32控制台应用程序下。那么当我在预处理器指令行中使用iostream时,我得到了..我四处搜索,并不明白为什么iostream不起作用,有什么帮助?Visual C++不允许iostream

 
1>------ Build started: Project: dd, Configuration: Debug Win32 ------ 
1> stdafx.cpp 1> dd.cpp 1>c:\documents and settings\leon\my documents\visual studio 2010\projects\dd\dd\dd.cpp(24): fatal error 
C1010: unexpected end of file while looking for precompiled header. 
Did you forget to add '#include "StdAfx.h"' to your source? 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
1 // This program calculates the user's pay. 
    2 #include <iostream> 
    3 using namespace std; 
    4 
    5 int main() 
    6 { 
    7 double hours, rate, pay; 
    8 
    9 // Get the number of hours worked. 
    10 cout << "How many hours did you work? "; 
    11 cin >> hours; 
    12 
    13 // Get the hourly pay rate. 
    14 cout << "How much do you get paid per hour? "; 
    15 cin >> rate; 
    16 
    17 // Calculate the pay. 
    18 pay = hours * rate; 
    19 
    20 // Display the pay. 
    21 cout << "You have earned $" << pay << endl; 
    22 return 0; 
    23 } 

回答

2

这不是因为iostream,而是因为你忘了,包括stdafx.h

这个错误很明显。如果您使用预编译头创建项目,则必须在实现文件的开头包含stdafx.h

+0

好的。它在我身上发现它有一种混乱。 – user1211008 2012-02-15 10:29:47

2

在Visual C++中,项目可以使用所谓的“预编译头文件”。这是一种帮助加速项目建设的技术。为此,您需要将文件“stdafx.h”作为您在源文件中执行的第一件事。

所以列入的iostream的前加入这一行:

#include "stdafx.h" 

当你再次构建它应该工作。

+0

我只是这样做,现在我失去了功能标题。 – user1211008 2012-02-15 10:25:00

+0

@ user1211008您是否添加了包含iostream的新行_before_?它必须始终是源文件中的第一个非注释行。您也可以尝试重建解决方案。 – 2012-02-15 10:30:52

+0

我找到了。放错地方;之后int main() – user1211008 2012-02-15 10:31:33

1

包括下面的头..

#include "stdafx.h" 
+0

ķ这样做TY求助 – user1211008 2012-02-15 10:43:00

-1

当您创建新的项目,而不是选择“W32控制台应用程序”,选择“空项目”,然后创建一个新的CPP文件,代码应该工作!

+0

会做那现在 – user1211008 2012-02-15 10:43:13

+0

我做到了,现在它无法找到指定的文件hours.exe,我调试源代码后。 – user1211008 2012-02-15 10:52:36

+0

我明白为什么它说,但我想你什么时候,调试它创建一个.exe? – user1211008 2012-02-15 10:58:18

0

试试这个:

#include "stdafx.h" 
#include "iostream"  

(删除扩展名为.h - VS2010似乎找不到与该文件扩展名的头文件)?