No escaping pits--header files are included with each other (Compiler error C2653:not a class or namespace name)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.