19

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?

Sensei James
  • 2,617
  • 30
  • 36
Kulis
  • 988
  • 3
  • 11
  • 25
  • 10
    Starting your project the Right Way is often 99% of the battle. File + New + Project, Visual C++, Win32, select "Win32 Console Application". You now have an stdafx.h file with all the compiler settings just the way they should be to use the precompiled headers feature. If the book didn't guide you that way then you ought to look for another one. But it probably did. – Hans Passant Oct 12 '14 at 21:53

6 Answers6

21

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.

ceztko
  • 14,736
  • 5
  • 58
  • 73
  • 1
    All the methods you defined in the header `vector.h` didn't find an imlemented symbol in the main executable. Ensure you are really compiling `vector.cpp`. In visual studio: ensure `vector.cpp` is in the "Source Files" filter (you see it as a folder). Or remove and readd it to the project. – ceztko Oct 13 '14 at 15:08
  • 1
    @ceztko "it's inadvisable to include the precompiled header in your own headers, to not clutter namespace of other source that can use your code" ... are you sure with that? Every precompiled header has `#pragma once`, so only the first one is used and the other (like in `vector.h`) are ignored, aren't they? – Martin Pecka Jun 10 '16 at 00:48
  • 1
    Hmm, I've arrived at a situation where having stdafx.h in my header files causes problems. That's when a library uses stdafx.h, but the program using it does not, and the stdafx.h file is not exposed by the library. – Martin Pecka Jun 11 '16 at 00:27
  • 1
    Yes. That should be also an issue. In general, I am concerned by the fact that stdafx.h exposes more than actually needed to link the library, so by including it unnecessarily you're polluting someone's else namespace. – ceztko Jun 11 '16 at 08:02
  • I suggest to not include stdafx.h textually. You can do it via command line args `/FI` in msvc. This allows you to compile your app with and without pch... For clang and gcc you can force an include, too. – Bernd Oct 27 '20 at 13:17
12

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

user3718058
  • 303
  • 1
  • 3
  • 11
8

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.

Dila Gurung
  • 1,726
  • 21
  • 26
  • 1
    I created those empty files in "solution number two" and in `Project Properties`-> `Precompiled Header` is left blank - I did not select `Create (/Yc)`. I rebuilt it and everything is ok. – Junior Mayhé Jul 21 '17 at 21:16
5

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.

Sar
  • 115
  • 1
  • 6
4

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

Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
Dila Gurung
  • 1,726
  • 21
  • 26
1

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.

Marius
  • 1,529
  • 6
  • 21