Home > Back-end >  Why class cannot like a function definition in the back of the main function?
Why class cannot like a function definition in the back of the main function?

Time:10-04

I read on the Internet explanation is:
1. Because the function declaration will know that the return type, you can use the return type at compile time allocated memory space, but not class,

Allocate space, I want to ask the compile time refers to the compile time for global variables or static variables are allocated space? Just only global or static variables need to be in the compiler allocates space?

In addition to the main function is only at run time space allocated on the stack, and compile time should also do not have what relation,


2. If the class definition on the back of the main function, the main function of the local object constructor of the class definition of what all don't know, could not instantiate,

I tried it really seems to be the problem, but should not be like function at compile time, into the symbol table? Or say that is not true?

For leaders to solve!

CodePudding user response:

Post your code????
Put the so-called doesn't define

CodePudding user response:

reference 1/f, the truth is more important than right or wrong response:
post your code???????
Put the so-called doesn't define

 class Test; 

Int main () {
Test the Test;
}

The class Test {
int x;
};

Error:
Error: aggregate 'Test Test' from the incomplete type and always be defined
Test the Test;

CodePudding user response:

Without the concept of class C
The c file renamed. CPP

CodePudding user response:

Advice look at c + +/c program generation process,,,

CodePudding user response:

The
reference 3 floor zgl7903 response:
C has no concept of class
The c file renamed. CPP

This code is in the CPP file ah,,,,

CodePudding user response:

The class Test;
Just forward declaration, is not really a statement, the real statement and. H the contents of the file,

CodePudding user response:

This kind of forward declaration belongs to incomplete type, the compiler only know it is a class, don't know what it has properties and member function, so can't use the statement object variables, to declare a pointer or a reference type,
  • Related