Why doesn't forward declaration work with classes?

int main() {
    B bb;                           //does not compile (neither does class B bb;)
    C cc;                           //does not compile

    struct t tt;                    //compiles

    class B {};                     //HERE is the class B defination
    struct s { struct t * pt; };    //compiles
    struct t { struct s * ps; };

    return 0;
}

class C {};

I just changed the example given here .

Why do struct forward declarations work but not class forward declarations?

Does this have anything to do with namespaces - tag namespaceand typedef namespace? I know that structure definitions without typedefs go to the tag namespace.

Structures are just classes with all public members. Therefore, I expect them to behave the same.

+3
source share
4 answers

Forward , , . -, class B; main. B * bb; B bb;. B .

: , B, ( ). B B, (, , ).

+7

; .

class B;

bb,

EDIT: kibibu, , ,

B* bb;

, .

+4

, "struct t tt;" .

++ , .

+1

:

struct t tt;

, :

.\TestApp.cpp(11) : error C2079: 'tt' uses undefined struct 'main::t'

( Visual ++ 2008)

+1

Source: https://habr.com/ru/post/1741387/


All Articles