这个问题是什么?“指向不完整类型'struct cashier'的指针”?

问题描述 投票:0回答:1

我正在学习队列的数据结构,并制作一个这样的收银员结构:1其中有2个整数,其中1个浮点数和1个队列数据类型。2因此,我想制作一个收银员指针来指向收银员结构。`

struct cashier {
    int numberOfCustomersServed; /* This should be initialized to 0 */
    int totalCustomerWaitingTime; /* This should be initialized to 0 */
    float totalAmountReceived; /* This should be initialized to 0 */
    queueADT customerQ; /* This should be initialized to an empty queue */
}cashier;

struct cashier* initCashier(void){
    struct cashier *y;
    y->numberOfCusCustomersServed=0;
    y->totalCustomerWaitingTime=0;
    y->totalAmountReceived=0.0;
    y->customerQ=getEmptyQueue();

    return y;
};

但随后出现错误:

/cygdrive/c/Users/Heta/Desktop/CLionHWQ2/supermarket.c:8:6: error: dereferencing pointer to incomplete type 'struct cashier'
     y->numberOfCusCustomersServed=0;

下面基本上是队列功能。3main()尚未完成,大部分只是空的。4任何帮助,将不胜感激。 :)

c pointers data-structures queue
1个回答
0
投票
struct cashier *y;

[y是指向struct cashier的指针,但未将其设置为指向类型为struct cashier的变量。]​​>

因此,

    y->numberOfCusCustomersServed=0;
    y->totalCustomerWaitingTime=0;
    y->totalAmountReceived=0.0;
    y->customerQ=getEmptyQueue();

    return y;

无效,因为y尚未初始化为指向类型为struct cashier的有效变量。


而是使用:

struct cashier* initCashier(void){

    struct cashier x;
    struct cashier *y = &x;

    y->numberOfCusCustomersServed=0;
    y->totalCustomerWaitingTime=0;
    y->totalAmountReceived=0.0;
    y->customerQ=getEmptyQueue();

    return y;
};
© www.soinside.com 2019 - 2024. All rights reserved.