Error C1083: Cannot open include file: 'stdafx

2020-02-05 13:05发布

When I compiled this program (from C++ Programming Language 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

#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

#include <stdafx.h>
class vector{
public:
    vector(int s);
    double& operator[](int i);
    int size();
private:
    double* elem;
    int sz;
};

It gave me these errors:

errors

I run it on Microsoft Visual Studio 2013, on Windows 7. How to fix it?

6条回答
Summer. ? 凉城
2楼-- · 2020-02-05 13:33

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.

Then, precompiled header is usually a per-project file in Visual Studio world, so:

  1. 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;
  2. 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.

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-05 13:35

Just running through a Visual Studio Code tutorial and came across a similiar issue.

Replace #include "stdafx.h" with #include "pch.h" which is the updated name for the precompiled headers.

查看更多
再贱就再见
4楼-- · 2020-02-05 13:36

You can fix this problem by adding "$(ProjectDir)" (or wherever the stdafx.h is) to list of directories under Project->Properties->Configuration Properties->C/C++->General->Additional Include Directories.

查看更多
Luminary・发光体
5楼-- · 2020-02-05 13:38

Just include windows.h instead of stdfax or create a clean project without template.

查看更多
走好不送
6楼-- · 2020-02-05 13:45

There are two solutions for it.

Solution number one: 1.Recreate the project. While creating a project ensure that precompiled header is checked(Application settings... *** Do not check empty project)

Solution Number two: 1.Create stdafx.h and stdafx.cpp in your project 2 Right click on project -> properties -> C/C++ -> Precompiled Headers 3.select precompiled header to create(/Yc) 4.Rebuild the solution

Drop me a message if you encounter any issue.

查看更多
The star\"
7楼-- · 2020-02-05 13:52

Add #include "afxwin.h" in your source file. It will solve your issue.

查看更多
登录 后发表回答