Undeclared identifiers with structs

对着背影说爱祢 提交于 2019-12-25 09:39:13

问题


struct FailedTransaction{
    OrderNodePtr order;
    int failureID;
    struct FailedTransaction* next;
    struct FailedTransaction* tail;
};
typedef struct FailedTransaction* FailedTransactionPtr;

struct SuccessfulTransaction{
    OrderNodePtr order;
    struct SuccessfulTransaction* next;
    struct SuccessfulTransaction* tail;
};
typedef struct SuccessfulTransaction* SuccessfulTransactionPtr;

struct FinalReport{
    FailedTransactionPtr failedTransactions;
    SuccessfulTransactionPtr successfulTransactions;
};

struct FinalReport* report = NULL;

This code is declared above main. When accessing

report->successfulTransactions

or

report->failedTransactions

I get undefclared identifier for FailedTransaction and SuccessfulTransaction.

Here is the code that manipulates report

if(report == NULL){
    report = malloc(sizeof(struct FinalReport));
    report->failedTransactions = NULL;
    report->successfulTransactions = NULL;
}
if(outcome){
    if(report->successfulTransactions == NULL){
        report->successfulTransactions = malloc(sizeof(SuccessfulTransaction));
        report->successfulTransactions->order = temp;
        report->successfulTransactions->tail = report->successfulTransactions;
    }else{
        report->successfulTransactions->tail->next = malloc(sizeof(SuccessfulTransaction));
        report->successfulTransactions->tail->next->order = temp;
        report->successfulTransactions->tail = report->successfulTransactions->tail->next;
    }
}else{
    if(report->failedTransactions == NULL){
        report->failedTransactions = malloc(sizeof(FailedTransaction));
        report->failedTransactions->order = temp;
        report->failedTransactions->tail = report->failedTransactions;
    }else{
        report->failedTransactions->tail->next = malloc(sizeof(FailedTransaction));
        report->failedTransactions->tail->next->order = temp;
        report->failedTransactions->tail = report->failedTransactions->tail->next;
    }
    report->failedTransactions->failureID = outcome;
}

The errors occur at the first lines after each if statements and else statements.

This is for an assignment and I have been stuck on this for an hour or so (it is due tomorrow night). Can't figure out why it is happening and I can't find anything online. Any help would be appreciated.

This is the header file that contains OrderNodePtr

#ifndef _CONSUMER_
#define _CONSUMER_

struct OrderNode{
    char title[250];
    int id;
    double cost;
    char category[250];
    struct OrderNode* next;
    struct OrderNode* tail;
};

typedef struct OrderNode* OrderNodePtr;

#endif   

回答1:


Try

sizeof(struct FailedTransaction);

Or, make FailedTransaction a typedef:

struct _FailedTransaction;
typedef struct  _FailedTransaction FailedTransaction;

struct _FailedTransaction {
    OrderNodePtr order;
    int failureID;
    FailedTransaction* next;
    FailedTransaction* tail;
};

Why does C need "struct" keyword and not C++?



来源:https://stackoverflow.com/questions/20182825/undeclared-identifiers-with-structs

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