c - 带有结构的未声明标识符

标签 c struct undefined-reference

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;

此代码声明在 main 之上。访问时

report->successfulTransactions

report->failedTransactions

我得到了 FailedTransaction 和 SuccessfulTransaction 的未声明标识符。

这是操作报表的代码

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;
}

错误发生在每个 if 语句和 else 语句之后的第一行。

这是一项作业,我已经坚持了一个小时左右(明晚到期)。无法弄清楚为什么会这样,我在网上找不到任何东西。任何帮助将不胜感激。

这是包含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   

最佳答案

尝试

sizeof(struct FailedTransaction);

或者,将 FailedTransaction 设为 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++?

关于c - 带有结构的未声明标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182825/

相关文章:

c++调用类的构造函数

c - operator++ 应用两次,具体取决于#define 函数的参数

c++ - 如何检查是否相等? (0 == i) 或 (i == 0)

char * 后无法释放内存

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 尝试实现堆栈时 C++ 中的 undefined reference

c++ - Perl 系统调用导致核心转储但 $?保持为零

go - 结构在 for 循环初始化器中

c - 如何引用变长结构

c - 对 struct tm 的数组进行排序