c++ - Forward declaration of struct -
i have header file a.h , inside have declared 1 structure. name of structure file. inside file have 3 members: a, b, c. in a.cpp have implemented structure , assigned values structure variable.
now have file b.h. inside have forward declaration of structure file. until point if compile it's not showing error when going access variable present in structure through b.cpp class give error "undefined struct".
what doing wrong?
what root cause of error?
when forward declare type, compiler treats incomplete type.
the forward declaration tells compiler said type exists , nothing more particular type.so, cannot perform action(like creating objects, or dereferencing pointers type) on type needs compiler know memory layout.
solution:
you cannot forward declare if need deference structure members, need include header file in source file.this ensure compiler knows memory layout of type. have design project accordingly.
Comments
Post a Comment