[英]Error: conflicting types for function, function declared in header file
所以我正在嘗試使用makefile和頭文件等編譯一個多文件程序。我有這個程序,我繼續得到相同的錯誤,即使我已經仔細檢查了一百萬次的類型。 救命!
search.c:11: error: conflicting types for 'search' search.h:1: note: previous declaration of 'search' was here
int search(struct intnode** root, int lookingfor, int* counter);
#include "search.h"
#include "intnode.h"
#include <stdlib.h>
#include <stdio.h>
/*
Usage:
search(root, value);
*/
int search(struct intnode** root, int lookingfor, int* counter) {
/*COMPARE TO ROOT KEY*/
/*IF EQUAL*/
if(int_compare(lookingfor, (*root)->key) == 0) {
printf("%d exists in tree", lookingfor);
counter++;
if ((*root)->R != NULL && (*root)->key == (*root)->R) {
search((*root)->R, lookingfor, *counter);
}
}
/*IF GREATER THAN AND THERE IS A CHILD*/
else if(int_compare(lookingfor, (*root)->key) == 1 && (*root)->R != NULL) {
search((*root)->R, lookingfor, *counter);
counter++;
}
/*IF LESS THAN AND THERE IS A CHILD*/
else if(int_compare(lookingfor, (*root)->key) == 2 && (*root)->L != NULL) {
search((*root)->L, lookingfor, *counter);
counter++;
}
return NULL;
}
確保在聲明search()
之前定義了intnode
。
#include "intnode.h"
應該放在search.h中。
從頭文件中包含.h + c代碼模塊所需的所有頭文件是一個好習慣。
在每個h文件中也使用標題保護:
#ifndef MY_HEADER_H
#define MY_HEADER_H
// code here
#endif // MY_HEADER_H
編譯代碼時,我得到的錯誤與您相同。 以及許多其他人。
主要問題是struct intnode
根本沒有定義。
也許它已定義但您尚未在此處發布其代碼。 但我們只能提供你所擁有的建議。 也許它是在intnode.h中定義的,在這種情況下,在search.h的開頭包含它可以解決這個特殊問題。
我編輯了那段代碼而沒有編譯錯誤。
主要的變化是定義了一個struct intnode
。
我定義它的方式是猜測。 你需要哪個struct intnode
取決於你,但你肯定需要一個。
還將search
返回類型更改為void*
。 並從這些行中刪除了*運算符for counter
:
search((*root)->R, lookingfor, *counter);
固定代碼。 search.h:
struct intnode
{
void* key;
struct intnode** R;
struct intnode** L;
};
void* search(struct intnode** root, int lookingfor, int* counter);
search.c:
include "search.h"
/* #include "intnode.h" */
#include <stdlib.h>
#include <stdio.h>
/*
Usage:
search(root, value);
*/
void* search(struct intnode** root, int lookingfor, int* counter) {
/*COMPARE TO ROOT KEY*/
/*IF EQUAL*/
if(int_compare(lookingfor, (*root)->key) == 0) {
printf("%d exists in tree", lookingfor);
counter++;
if ((*root)->R != NULL && (*root)->key == (*root)->R) {
search((*root)->R, lookingfor, counter);
}
}
/*IF GREATER THAN AND THERE IS A CHILD*/
else if(int_compare(lookingfor, (*root)->key) == 1 && (*root)->R != NULL) {
search((*root)->R, lookingfor, counter);
counter++;
}
/*IF LESS THAN AND THERE IS A CHILD*/
else if(int_compare(lookingfor, (*root)->key) == 2 && (*root)->L != NULL) {
search((*root)->L, lookingfor, counter);
counter++;
}
return NULL;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.