Recently by oneself pit son of a, change someone's code, the final compiler such error:
Error c2653:not a class or namespace name
Briefly describe:
There is a class A, declaration and implementation in A.H and a.cc, respectively
There is a class B, declaration and implementation in B.h and b.cc, respectively
A member function of Class A requires a Class B type as a parameter
A member function of Class B requires a class A type as a parameter
The A.h file is as follows:
#ifndef A_H_#define A_H_#include "b.h"class A{public: enum State { 0; 1; } void send (B b); ...};#endif
a.cc file omitted, no impact
B.h file is:
#ifndef B_H_#define B_H_#include "a.h"class B{public: void send (A::State state); ...};
At this point, the compiler will error:
Error C2653:A not A class or namespace name
Our first response is often to see if the B.h contains the header file that contains the Class A, that is, whether the A.h file is included.
But the pit father is, we clearly contain AH, how can still error?
Well, that's what's involved with each other, and the compiler has been blindfolded.
In short, do not include unnecessary header files.
I almost forgot to tell you how to change it?
First, we put classes A and B in a namespace, which is namespace keng{} encapsulation for both A and B.
So at this point, we can change the header file without B in a to the predecessor Declaration 、、、、、
#ifndef A_H_#define A_H_class B;class A{public: enum State { 0; 1; } void send (B b); ...};#endif
Done!
We used #ifndef to avoid the repeat contains the head file of the pit, but we can not unbridled inclusion, but also to prevent the case of mutual inclusion!!!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
No escaping pits--header files are included with each other (Compiler error C2653:not a class or namespace name)