错误C1083:无法打开包含文件:'stdafx.h' [英] Error C1083: Cannot open include file: 'stdafx.h'
问题描述
当我编译此程式(从Programmin Launguage第4版):
When I compiled this program (from Programmin Launguage 4th edition):
main.cpp
#include <stdafx.h>
#include <iostream>
#include <cmath>
#include "vector.h"
using namespace std;
double sqrt_sum(vector&);
int _tmain(int argc, _TCHAR* argv[])
{
vector v(6);
sqrt_sum(v);
return 0;
}
double sqrt_sum(vector& v)
{
double sum = 0;
for (int i = 0; i != v.size(); ++i)
sum += sqrt(v[i]);
return sum;
}
vector.cpp
$ b
vector.cpp
#include <stdafx.h>
#include "vector.h"
vector::vector(int s)
:elem{ new double[s] }, sz{ s }
{
}
double& vector::operator[](int i)
{
return elem[i];
}
int vector::size()
{
return sz;
}
vector.h
$ b
vector.h
#include <stdafx.h>
class vector{
public:
vector(int s);
double& operator[](int i);
int size();
private:
double* elem;
int sz;
};
它给了我这些错误:
我在Microsoft Visual Studio 2013上运行,如何解决?
I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?
推荐答案
你必须正确理解什么是stdafx.h预编译头。其他问题或维基百科将回答。在许多情况下,可以避免预编译头,特别是如果您的项目很小并且具有很少的依赖性。在你的情况下,你可能从一个模板项目开始,它被用来包括 Windows.h
只有 _TCHAR
宏。
You have to properly understand what is a "stdafx.h", aka precompiled header. Other questions or Wikipedia will answer that. In many cases a precompiled header can be avoided, especially if your project is small and with few dependencies. In your case, as you probably started from a template project, it was used to include Windows.h
only for the _TCHAR
macro.
然后,预编译头通常是Visual Studio世界中的每个项目文件,因此:
Then, precompiled header is usually a per-project file in Visual Studio world, so:
- 确保您的项目中有文件stdafx.h。如果你没有(例如你删除它)只是创建一个新的临时项目并复制默认的项目;
- 更改
#include< stdafx。 h>
到#includestdafx.h
。它应该是一个项目本地文件,不能在include目录中解析。
- Ensure you have the file "stdafx.h" in your project. If you don't (e.g. you removed it) just create a new temporary project and copy the default one from there;
- Change the
#include <stdafx.h>
to#include "stdafx.h"
. It is supposed to be a project local file, not to be resolved in include directories.
其次:不能包含预编译头在你自己的头文件中,不要混淆其他来源的命名空间,可以使用你的代码作为库,因此完全删除它包含在 vector.h
。
Secondly: it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code as a library, so completely remove its inclusion in vector.h
.
这篇关于错误C1083:无法打开包含文件:'stdafx.h'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!