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 :)