簡體   English   中英

這有什么問題? C

[英]What is wrong with this? C

我找不到我的錯誤。 這是structures.h的定義

typedef struct book {
  bank_account_t **accounts;
  transaction_t **transactions;
} book_t;

這是在functions.c ,其中包含標頭並嘗試使用book_t類型

#include "structures.h"

void load_book(book_t *book) {

}

但是我得到這個錯誤

functions.c:10:16: error: unknown type name ‘book_t’
 void load_book(book_t *book) {
                ^

使用以下代碼進行編輯:

在我的main文件中,我按如下順序訂購.h文件

#include "structures.h"
#include "functions.h"

structures.h

#ifndef STRUCTURES_H
# define STRUCTURES_H


typedef struct bank_account {
  char *name;
  int amount;
} bank_account_t;

typedef struct transaction {
  char *name;
  int amount;
} transaction_t;

typedef struct book {
  bank_account_t **accounts;
  transaction_t **transactions;
} book_t;

#endif

function.c

#include <stdio.h>

#include "functions.h"
#include "structures.h"
#include "bank_account.h"
#include "transaction.h"

void load_book(book_t *book) {

}

void init_book() {

}

bank_account.h

#ifndef BANK_ACCOUNT_H
# define BANK_ACCOUNT_H

void init_new_bank();
void deinit_new_bank();

#endif

transaction.h

#ifndef TRANSACTION_H
# define TRANSACTION_H

#endif

我認為這個問題必須在functions.h(其中不包括在原來的職位)。

functions.h

#ifndef FUNCTIONS_H
# define FUNCTIONS_H

/* [MarkU] required: include definition of book_t */
#include "structures.h"

void load_book(book_t *book);
void init_book();

#endif

沒有#include structures.h includestructures.h,就沒有boot_t類型的定義。

使用mingw32-gcc 4.7.2構建並驗證。 省略#include,我看到錯誤消息。

functions.c更改其順序:

#include "functions.h"
#include "structures.h"

成為

#include "structures.h"
#include "functions.h"

微妙的是,錯誤消息起源於functions.c而不是functions.h

假設protoytpe到load_book(book_t *)functions.h ,它需要了解book_t

所以這個最佳的解決方案是將包括structures.hfunctions.h (如也已經指出MarkU 答案 )。

經驗教訓 :始終(且僅)包括您需要的內容以及需要的位置。 避免(微妙的)依賴性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM