1

I am new here, sorry but there are many similar topics but they don't answer the question right.

(The other thing is, that many people says the #include "stdafx.h" should be placed in every .h file AND .cpp file, I added them in every .h file and everything works fine. I know what happens when the #include "stdafx.h" is only included in the .cpp files If I place them only in the .cpp file, I guess some classes or enums are not defined, and I want them as member in a class or inherit from them... Here is the wall of text:)

Here is the topic which I found where the "right" questions was asked, but nobody answered the "right" question, only the "basics" behind the pchs.

include stdafx.h in header or source file?

I have a similar question, I know the pch and how they work, but the question Marnix wasn't solved completely.

His question was:

stdafx contains freeglut. my class header file has an attribute of GLenum.

Should I include the stdafx into the .h of the class?

I know you could declare a "undefined class" with class B as example, but what are you doing when you want to derivate from this class, or using that as a local variable, not as a pointer in the header file.

Example:

//Where B is in the precompiled header file, let's say its a big class from <freeglut.h> 

class B1;

class c : public B // B is unknown and wont "compile" here
{
public:
    void test2();
        B1 tes; // Won't work because of undefined class..., I know why
        B1* tes2; // Works fine, I know why
};

The header file is incorrect because B isn't known yet. Is it a good way to include here the "stdafx.h" file - or even include the freeglut.h ?!

I guess no, but let's say the desired class B is in a namespace, somewhere hidden...

So, the header won't "compile" due to the errors. And I don't want to add the header file to the "stdafx.h", because maybe I change it a lot..

I know what pchs are and how they work.

Do you know a solution to it?

  • This is the unsolved question (including my question) from Marnix, look at the link.

UPDATE:

Bill the Lizard removed my description of the problem - it should be describe my problem better. Thanks for that.

The problem isn't solved yet.

The both answers are on the main problem, and doesn't solve it. Just test it, it won't work. I even asked my prof for an answer - he didn't know an answer to it. So:

Here is the problem described in easy words:

I have a huge class A. This class is very big and doesn't change, so I include them in the precompiled header.

Now I have a class B which inherits from class A, and have a local member of the class A.

The problem is that class A doesn't know class B. The declaration of class A; doesn't help, because I want to inherit from it and want a local member of class A inside class B.

Everybody says that the precompiled header should be "ONLY" in the .cpp file - so it would include everything (while the preprocessor is active) and everything should work, because this is the very first line for the preprocessor. Nonsense.

So- just think about it. It cant handle it because the header file doesn't now the class B, even when the .cpp file have the definition of the class B thru the precompiled header.

Sample code:

  // Class A is inside the precompiled header - with #include "classA.h"
class A
{
    //BIG BIG BIG class
};


//This class doesn't know class A because I didn't include the precompiled header
//And it doesn't make sense to include the "big" header file from class A

//class A; // fake class A
class B : public A // A is unknown and won't compile even when "fake class A" is declared
{
    A nonUseableVariable; // A is unknown unless you include the header or precompiled header for it;
    A* ptr; // Only member works when "fake class A" is declared
};

//Solution from everybody how doesnt read and understand the real problem
//This is the .cpp file from class b.cpp
#include "stdafx.h" // dont care, the header cant be compiled

Let's say the header of class B is changing often, or the class A is somewhere in a namespace, there is no solution to this problem, you must add the precompile header here.

So I hope you understand me now. There is no other solution to it I guess, because class A have to be known for class B due to the inheritance and the local member, when the member is a pointer, the member works.

But look into the code.

I hope someone could help me here, and no admin remove this again.

UPDATE: SOLVED

VS was going crazy, after a reboot of the whole system, everything works fine. Thank you again :)

Dip Hasan
  • 225
  • 3
  • 9

1 Answers1

1

When using precompiled headers, they must be the first include of any implementation (.cpp) file - otherwise the compiler will complain.

Ergo there is no need to ever include them in the header file, as they will always be present as the first include of all implementation files.

Edit

And as Joachim Pileborg points out, it must be the first non-comment in the source file!

Edit 2

As a proof of concept, I created a project:

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here
#include "a.h"

a.h

#pragma once

class A
{
public:
    A();
};

a.cpp

#include "stdafx.h"

A::A()
{
}

b.h

#pragma once

class B : public A
{
public:
    B();
}

b.cpp

#include "stdafx.h"
#include "b.h"

B::B()
{
}

Output:

========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Caveats:

Intellisense doesn't like it, but that's not entirely surprising - the point is, it does work.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Not only first included header file, it must be the first non-comment or non-blank line in the source. – Some programmer dude Nov 21 '13 at 11:04
  • Nope, it doesnt solve the problem, and you did exactly that what everybody else did, like I said. A new description is there (but the same old problem), please look into the UPDATED part. – ScreenOfDeath Nov 21 '13 at 14:55
  • @ScreenOfDeath, I have read the updated part. Think about it. Because stdafx.h *has* to be included before *anything* other than a comment in a .cpp file, the definition of class A will *always* appear before any other header file is included, including that of `B`. – Moo-Juice Nov 21 '13 at 14:57
  • @Moo-Juice, I´ve test it, my professor test it ,please test it too. Could you explain how the compiler should know the size of class A for the member? There is no other way to solve it. Please take 2 minutes for coding it, and test it. – ScreenOfDeath Nov 21 '13 at 14:59
  • Wow. My VS was going crazy with it. Thank you for proofing it, I guess I have to say sorry! Nice one :D Hm, maybe it was a bug ( and of course, intellisense doesnt like it.) I must call my professor :D I just restared the whole system and everything works like yours. Nice error. – ScreenOfDeath Nov 21 '13 at 15:10
  • @ScreenOfDeath, excellent! I guess VS got itself in a tizzy :) – Moo-Juice Nov 21 '13 at 15:14