Sumanto's blog

By Sumanto, history, 4 years ago, In English

My earlier code which was giving error: complete implementation here: https://ideone.com/MuOmlS

Code giving compilation error

This Code giving error as:

ERRORS

correct code after referring this thread https://stackoverflow.com/questions/12466055/field-has-incomplete-type-error/12466219 i implemented and it worked but from this thread i couldn't able to understand why pointer object is doing which leads to removal of error?

complete implementation here: https://ideone.com/SSVnOU

Correct code ( doubt is don't know why working correctly)

UPDATE:

After referring some blogs i came to know there is also an another way of handling this case

Different implemetation (correct)

I Believe i am missing some concept of c++ language which i need to know to understand what's going on here. Please share your views regarding this.

  • Vote: I like it
  • -8
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

The StackOverflow page you referred to had the answer. I'll just reiterate what was written there:

When you do forward declaration i.e only declare but not define, then the compiler just knows it'll exist, its definition is provided on link time. Now why it affects the way it did here: The compiler doesn't know the size of the object to allocate, since it's not defined, so we can only use a pointer which is always going to be the size of a memory address(4 bytes usually). We actually do this (forward declaration then using a pointer) in PIpml idiom too, if you've done that you can relate from there as well.

Hope it helps.

P.S: I'm not a Cpp expert(unlike my name suggests) so I might have said something wrong, but I think I explained the main question.