Regarding the error caused by using sizeof (class) in C ++

When I compile my project in C ++, MSVC produces the following error:

error No. 94: array size must be greater than zero

The error occurs on the following line when executing sizeof:

if (sizeof (MyNamespace::MyClass) == 60) 

MyClass is defined this way:

 class MyClass: public ParentClass { public: MyClass( void *pCreate, int a, int b, bool c) : ParentClass( pCreate, a, b, c ) {} virtual inline void myFunc ( ) { //something } private: virtual ~MyClass(){}; /** * Copy assignment. Intentionally made private and not implemented to prohibit usage (noncopyable stereotype) */ MyClass& operator=(const MyClass&); }; 

Can someone tell me what could be wrong? Even if sizeof returns zero size, why is this a compiler error?

+4
source share
2 answers

This error occurs when you take the sizeof class that is only declared at this point. For instance. class MyClass; const size_t error = sizeof(MyClass); .

Note that it does not matter if the class is fully defined later: the definition must precede sizeof .

+10
source

This error is most likely caused by a forward declaration. In the line where you use sizeof, the compiler should know the definition of your MyClass class, that is, you must include the # header file for it

+2
source

All Articles