An error occurred while referencing ahead (class advance declaration): Error c2079: 'xxx' uses undefined class '***'

Source: Internet
Author: User

The error is as follows:
Error c2079: 'xxx' uses undefined class '***'

For example, we first define a class B and then a Class A. A member of Class A is B, as shown below:

Class B

{
Int I;
};

Class

{
B val;
};

Of course;

 

However, if we need to put the definition of B at the end, we all know that we should declare B before, then:

Class B;

Class {
B val;
};

Class B {
Int I;
};

Right? It seems that there is no error. It is a bit like declaring and calling a function in advance;

In fact, compilation fails;

 

Because at this time: Class B has not yet been allocated space, and Val cannot be defined and allocated space (because the compiler needs to allocate space for its members when processing the class );

Another person came to the conclusion that class names cannot be used for advanced reference to define variable parameters of variables and functions. They can only be used to define references or pointers.

 

Then, replace Val with the pointer to B:

Class B;

Class {
B * val;
};

Class B {
Int I;
};

 

Bingo!

 

 

 

Another interesting question is:

Class B;
Class;

Class {
B val;
};

Class B {
A val;
};

In this case, the two sides of the mirror are placed in the same way ..

When the Val of A is processed, it is found to be B, so leave space for B,
In order to know how much space is reserved for B, let's look at B and find that the Val of B is,
Then you need to know how much space A should be given...
At this time, an error occurs: Error c2079: 'xxx' uses undefined class '***'.

 

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.