Why am I getting this error: dereferencing pointer to incomplete type

半城伤御伤魂 提交于 2019-12-04 18:42:54

Why am I getting this error: dereferencing pointer to incomplete type?

Instead of including the header file you add the line:

struct task_struct;

It Forward declares the class task_struct which means for compiler it is an Incomplete type. With Incomplete types, One cannot create variable of it or do anything which needs the compiler to know the layout of task_structor more than the fact that task_struct is just an type. i.e: The compiler does not know what are its members and what its memory layout is.
But Since pointers to all objects need just the same memory allocation, You can use the forward declaration when just reffering to an Incomplete type as a pointer.

Note that forward declarations are usually used in case where there is a Circular Dependency of classes.

Forward Declaration has its own limitations on how the Incomplete type can be used further on.
With Incomplete type you can:

  • Declare a member to be a pointer to the incomplete type.
  • Declare functions or methods which accepts/return incomplete types.
  • Define functions or methods which accepts/return pointers to the incomplete type (but without using its members).

With Incomplete type you cannot:

  • Use it to declare a member.
  • Define functions or methods using this type.

Proposed Solution:
The problem here because inside the function initschedule You try to access the members of the struct task_struct, namely:

(seedTask)->thread_info->processName

So the compiler here needs to know the definition of member thread_info. You need to include the header which defines struct thread_info in your schedule.c.

Why it works without error in schedule.h?
Because that header file only references the struct thread_info as an pointer and none of its member, while schedule.c does.

cnicutar

It's because the function initschedule has only seen a declaration of struct task_struct, not its definition. Presumably the function that calls initschedule has seen the definition and can therefore dereference the pointer to it.

So for initschedule, it's an incomplete type, it can only pass pointers to it around but cannot actually dereference those pointers.

Just make sure you include the header that defined that struct in schedule.c.

EDIT

It seems it's another incomplete definition that is causing this: thread_info. It's the same basic problem as explained above, but you have to include the header that defines thread_info.

By the time you define initschedule, task_struct is still an incomplete type. You need to make sure that the whole definition of struct task_struct is visible when initschedule is compiled.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!