【问题标题】:Undeclared identifiers with structs带有结构的未声明标识符
【发布时间】:2013-11-25 00:12:24
【问题描述】:
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 和 SuccessTransaction 的未声明标识符。

这是操作报告的代码

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   

【问题讨论】:

  • 不认为我需要添加它,但既然你问我会的。

标签: c struct undefined-reference


【解决方案1】:

试试

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++?

【讨论】:

  • 哦,哇,我现在感觉很笨。谢谢你。忘了把 struct 放在 malloc 语句中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-21
  • 2010-11-30
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
相关资源
最近更新 更多