C ++ error set: XX does not name a type; field 'xx' has incomplete type

Source: Internet
Author: User

Two C ++ compilation errors and solutions -- does not name a type and field 'xx' has incomplete type

Compilation Error 1: XX does not name a type

Compilation Error 2: Field 'xx' has incomplete type

 

Compilation Error 1: XX does not name a type, which means "XX does not name a type".

For example, if two classes are defined as follows:

Class B {

Public:

B (){}

~ B (){}

PRIVATE:

A;

};

Class {

Public:

A (){}

~ A (){}

PRIVATE:

Int;

};

When compiled, an error is reported: "A does not name a type"

The error is reported on the red line.

Even if clase A and Class B are defined in two files, this error is also reported when the # class a header file is included in the file header defining B (this is caused by the sequential relationship between compilation and links ).

Solution:

Before class B's definition declaration, declare Class A as follows:

Class;

Class B {

Public:

B (){}

~ B (){}

PRIVATE:

A;

};

Class {

Public:

A (){}

~ A (){}

PRIVATE:

Int;

};

 

Compilation Error 2: Field 'xx' has incomplete type

In the same way as in the above example, the first error has disappeared through problem 1, but the second error will appear immediately! Or the same location.

This error means that the XX domain type in Class B is incomplete. Why? It is clearly defined after Class. In fact, the reason is the same as the previous one. Before class B was defined, we only declared Class A without specific definitions. So the solution to this error is as follows:

Use the pointer instead of the field in the Class B definition. After correctionCodeIs:

Class;

Class B {

Public:

B (){}

~ B (){}

PRIVATE:

A *;

};

Class {

Public:

A (){}

~ A (){}

PRIVATE:

Int;

};

In this way, this simple code can be compiled without errors.

Related Article

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.