0
0
0142016-02-27 19:31:06
C++ / C#
014, 2016-02-27 19:31:06

Why is there an error when executing sizeof()?

When compiling, it complains about the int size() function:
error: invalid application of 'sizeof' to incomplete type 'test1'
return sizeof(test1);
^

class test1;
class test
{
private:
    int i;
    char str[20];
public:
    int size()
{
    return sizeof(test1);
}

};
class test1: public test
{
public:
    int testq()
    {
      return sizeof(test);
    }
};

but if I place this function outside the class itself (int test::size()), then everything is fine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2016-02-27
@gbg

Everything is fair - at the time of accessing sizeof, the compiler does not have a complete declaration of the test1 class.
In C++, only templates should be placed in the body of a class.
**about inline-functions** - inline is only *advice to the compiler*, and not the command "put the body of the function into the call site"

J
jcmvbkbc, 2016-02-27
@jcmvbkbc

Because sizeof(test1) is used before the test1 class is defined.
And try to place it above the definition of class test1.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question