簡體   English   中英

typedef 重定義不同類型('struct word' vs 'struct word')};

[英]typedef redefinition with different types ('struct word' vs 'struct word') };

我在完成這項任務時遇到了困難,每當我使用其他結構創建鏈表時,我都會收到錯誤消息。

鏈表

#include "sentence.c"

typedef struct{
    sentence *first;
    int numSentences;
}LinkedList;

句子c

#include "word.c"

typedef struct{
  word *sentence;
  int lineNumber;
  int numWords;
}sentence;

字.c

typedef struct{
  char *word;
  int numCharacters;
  int wordPos;
}word;

我只有在實現鏈表部分時才會遇到問題,當我使用句子和單詞結構進行編譯時,它們工作正常。 我沒有在任何地方初始化鏈表,它仍然為句子和單詞給出這個錯誤:

在此處輸入圖片說明

我不太確定這個錯誤意味着什么,因為我不相信我會在其他任何地方重新聲明它。

  • 永遠不要#include .c 文件。 沒有例外。
  • 將其他文件需要訪問的typedef放在.h文件中,例如word.h或sentence.h。
  • 為結構成員提供與其所在結構的類型相同的標識符並不是一個絕妙的主意。

你不應該 #include ".c" 文件。 #include 僅適用於“.h”文件。

但這很可能不是錯誤的主要原因,盡管不這樣做會簡化故障排除。

您很可能在其他 2 個文件中包含 word.c - 直接或間接

您還應該習慣於為頭文件添加包含保護,以修復同一頭文件的多個包含。

暫無
暫無

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

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