I had an issue of circular references (i.e. A.h and B.h #including each other) and some people advised me to use #pragma once to prevent that. However, this solution appears to be not working still.
What's happening is that class A no longer becomes recognized in any file other than A.h (not even in A.cpp) and the same happens for class B.
Let me show you the code:
A.h
#pragma once
#include "B.h"
class A {
public: B* b;
};
B.h
#pragma once
#include "A.h"
class B {
public: A* a;
};
A.cpp
#include "stdafx.h"
#include "A.h"
#include "B.h"
B.cpp is same as A.cpp
The error trace reads as followed:
1> B.cpp 1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C2143: syntax error :
missing ';' before '' 1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int
1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int 1>
A.cpp 1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C2143: syntax error :
missing ';' before '' 1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int
1>c:usersuserdocumentsvisual studio
2010projectsenvmodel est.h(5): error C4430: missing type
specifier - int assumed. Note: C++ does not support default-int
There are two more things I'd like to clarify:
How and when to use #include "stdafx.h" in Visual Studio 2010 (I really hate this IDE, but it appears to be the only one that works well with C++ - Netbeans is total trash with this language)
How to use #pragma once correctly? I'd assume I'd just put it at the first line of each file (well, obviously that doesn't solve my problems!). Also, should it be placed before or after #include "stdafx.h"? Note: I didn't put #pragma once in stdafx.h
Thanks.
Edit: I forgot the semi-colons, thus the original error trace was bloated.
Edit2: I forgot to use pointers. My actual program did use pointers instead of plain object values and I neglected that in my haste to create a small example.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…