1

Let's assume I have a class Person

TestClass.h:

#pragma once

class Person {
    int age;
    std::string name;

public:
    Person(int age, const std::string& name);

private: 
    int getAge();
    std::string getName();
};

TestClass.cpp

#include "stdafx.h"
#include "TestClass.h"

Person::Person(int age, const std::string& name) : age(age), name(name) {};

int Person::getAge() {
    return age;
}

std::string Person::getName() {
    return name;
}

For this example, the code compiles. However, if I switch between line 1 and line 2 in TestClass.cpp and include stdafx.h second, for some reason I get the following compilation errors:

'Person': is not a class or namespace name  
missing type specifier - int assumed. Note: C++ does not support default-int    
'Person': constructor initializer lists are only allowed on constructor definitions 
'Person': is not a class or namespace name  
'Person': function should return a value; 'void' return type assumed    
'age': undeclared identifier    
'Person': is not a class or namespace name  
'name': undeclared identifier

Obviously if I just include stdafx.h first I won't have any problems, but I can't seem to understand why this happens. Any help would be appreciated.

Daniel
  • 631
  • 9
  • 20
  • 2
    stdafx.h is a weird Visual-Studio-specific file with special meaning and behaviour: [What's the use for “stdafx.h” in Visual Studio?](http://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio) If it insists on going first, let it. Visual Studio won't compile anything in the file that comes before stdafx.h. Because they say so. – user4581301 Mar 05 '17 at 00:36
  • @user4581301 This is what I was looking for. I know that stdafx.h is a VS specific file and I figured that this was probably on purpose but I wasn't able to find anything that explicitly said that nothing can come before the file's include. Thanks. – Daniel Mar 05 '17 at 00:43
  • With you on that lack of a from the horses mouth explanation.Best I can do for an authoritative answer is the previous link – user4581301 Mar 05 '17 at 00:50
  • if you don't need stdafx.h, better select **empty project** while creating a new one. That way standard C++ files will compile normally without including stdafx.h – phuclv Mar 05 '17 at 01:10
  • @LưuVĩnhPhúc I know how to get the code to compile, the point of the question was so I could understand why the code won't compile if stdafx.h isn't in the first line. This is assuming that stdafx.h is needed. – Daniel Mar 05 '17 at 01:19

1 Answers1

0

Do the following:

  1. #include <string> into TestClass.h
  2. disable precompiled headers

Then it should work.

Laszlo
  • 769
  • 9
  • 19
  • As I mentioned in a comment and in the actual question, I know how to get the code to compile. The question wasn't how to get the code to compile, It was why the code doesn't compile if stdafx.h isn't in the first line. – Daniel Mar 05 '17 at 01:22
  • I tried your code, and it compiles fine with these changes. Even if stdafx.h is included after TestClass.h – Laszlo Mar 05 '17 at 01:26
  • What were you using to compile the code? I use VS 2015 and it didn't compile. If you are using VS it doesn't really make sense that the code compiled since VS won't compile anything that comes before stdafx.h unless the compile option /Yu'stdafx.h' is unchecked as mentioned in this answer http://stackoverflow.com/a/16040876/5513449 – Daniel Mar 05 '17 at 01:29
  • I was using VS2013. '/Yu unchecked' means 'disable precompiled headers'. This recommendation is for lazy programmers like myself. The proper solution is to recreate the .pch file e.g by deleting it, after you have changed the order of the include files. After recreating the .pch file it will compile just fine. – Laszlo Mar 05 '17 at 01:38