为什么我收到此错误:提领指向不完全类型 [英] Why am I getting this error: dereferencing pointer to incomplete type

查看:215
本文介绍了为什么我收到此错误:提领指向不完全类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个项目工作类可能是最糟糕的指令曾经在这里我们要实现一个简单的调度。虽然C编程是不是该课程一preREQ,即调度和我的语言M不一定是一个C程序员。

不管怎样,我试图通过打印任务,这样我就可以跟踪它通过程序来调试,但我不断收到以下编译时错误:


  

schedule.c:61:48:错误:提领指向不完全类型


这是的task_struct 定义:

 结构的task_struct
{
    结构的thread_info *的thread_info;
    INT PRIO,static_prio,normal_prio;
    无符号长sleep_avg;
    无符号长长last_ran;
    无符号长长的时间戳;
    无符号长长sched_time;
    unsigned int类型time_slice,first_time_slice;
    结构LIST_HEAD run_list;
    结构sched_array *阵列;
    枚举sleep_type sleep_type;
    INT need_reschedule;
};

功能我尝试内部调试:

 无效initschedule(结构运行队列* newrq,结构的task_struct * seedTask)
{
的printf(内部initschedule()\\ n);的printf(%S - TEST \\ n,(seedTask) - GT; thread_info-> processName); //导致编译器错误/ *初始化运行队列和当前任务* /
RQ = newrq;
电流= NULL;/ *为运行队列调度数组分配内存* /
rq->积极=(结构sched_array *)malloc的(的sizeof(结构sched_array));
rq->过期=(结构sched_array *)malloc的(的sizeof(结构sched_array));/ *初始化数组时间表* /
INIT_LIST_HEAD(安培; rq->主动 - >清单);
INIT_LIST_HEAD(安培; rq->主动 - >清单);在调度* / *激活任务/
activate_task(seedTask);

}

最奇怪的是,当我使用相同的的printf(...)在调用上面的函数,它的工作原理,但只是没有在我的函数方法其中 seedTask 被传入。

所以基本上,我想知道为什么我收到此错误?


解决方案

  

为什么我收到此错误:提领指向不完全类型


相反,包括头文件中添加一行:

 结构task_struct的;

转发声明的task_struct ,这意味着编译器它是一个不完全型。使用不完全类型,不能创建它的变量或做任何事情,它需要编译器知道的task_struct 的布局或以上的事实,的task_struct 只是一个类型。
即:编译器不知道是什么的成员以及它的内存布局是结果。
但由于指针的所有对象只需要相同的内存分配,可以使用前置声明时刚刚reffering不完全类型的指针。

请注意,转发声明通常情况下,使用那里是类循环依赖。

向前声明对如何在不完全类型可进一步上使用其自身的局限性。结果
不完全类型,您可以:


  • 声明一个成员是一个指向不完全类型。

  • 声明它接受函数或方法/返回不完全类型。

  • 定义,它接受的函数或方法/返回指向不完全类型(但不使用它的成员)。

使用不完整的类型不能:


  • 使用它来声明一个成员。

  • 定义使用这种类型的函数或方法。

建议的解决方案:结果
这里的问题是因为函数内部 initschedule 您尝试访问结构的task_struct ,即成员:

 (seedTask) -  GT; thread_info-> processName

所以编译器在这里需要知道会员中的thread_info 的定义。您需要包括定义结构的thread_info schedule.c

标题

为什么它的工作原理没有错误在 schedule.h ?结果
由于该头文件只引用结构的thread_info 为指针,没有它的成员,而 schedule.c 一样。

I am working on a project for class with probably the WORST instruction ever where we have to implement a simple scheduler.. although C programming is not a prereq for the course, that is the language of the scheduler and I'm not necessarily a C programmer..

Anyhow, I am trying to debug this by printing out the task so I can trace it through the program, but I keep getting the following compile-time error:

schedule.c:61:48: error: dereferencing pointer to incomplete type

This is the task_struct definition:

struct task_struct
{
    struct thread_info *thread_info;        
    int prio, static_prio, normal_prio;     
    unsigned long sleep_avg;            
    unsigned long long last_ran;            
    unsigned long long timestamp;           
    unsigned long long sched_time;          
    unsigned int time_slice, first_time_slice;  
    struct list_head run_list;          
    struct sched_array *array;          
    enum sleep_type sleep_type;         
    int need_reschedule;                
};

The function im trying to debug inside:

void initschedule(struct runqueue *newrq, struct task_struct *seedTask)
{
printf("Inside initschedule()\n");

printf("%s - TEST \n", (seedTask)->thread_info->processName); //causes compiler error

/* initialize runqueue and current task */
rq = newrq;
current = NULL;

/* allocate memory for runqueue schedule arrays */
rq->active = (struct sched_array*)malloc(sizeof(struct sched_array));
rq->expired = (struct sched_array*)malloc(sizeof(struct sched_array));

/* initialize schedule arrays */
INIT_LIST_HEAD(&rq->active->list);
INIT_LIST_HEAD(&rq->active->list);

/* activate task in scheduler */
activate_task(seedTask);

}

The strangest thing is that when I use the same printf(...) in the method that calls the above function, it works, but just not in my function where the seedTask is passed into.

So basically, I am wondering why am I getting this error?

解决方案

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.

这篇关于为什么我收到此错误:提领指向不完全类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆