简体   繁体   English

错误:函数的冲突类型,头文件中声明的函数

[英]Error: conflicting types for function, function declared in header file

so I'm trying to compile a multifile program using makefiles and header files etc. I've got this one program that I keep on getting the same error for even though I've double checked the type a million times. 所以我正在尝试使用makefile和头文件等编译一个多文件程序。我有这个程序,我继续得到相同的错误,即使我已经仔细检查了一百万次的类型。 Help! 救命!

Errors: 错误:

search.c:11: error: conflicting types for 'search' search.h:1: note: previous declaration of 'search' was here

This is my .h file 这是我的.h文件

int search(struct intnode** root, int lookingfor, int* counter);

This is my .c file 这是我的.c文件

#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" should be placed in search.h. #include "intnode.h"应该放在search.h中。

It is a good habit to include all needed header files needed for your .h + c code module from the header file. 从头文件中包含.h + c代码模块所需的所有头文件是一个好习惯。

Also use header guards in every h file: 在每个h文件中也使用标题保护:

#ifndef MY_HEADER_H
#define MY_HEADER_H

// code here

#endif // MY_HEADER_H

I get the same error as you when compiling your code. 编译代码时,我得到的错误与您相同。 As well as many others. 以及许多其他人。

The main issue is that struct intnode is not defined at all. 主要问题是struct intnode根本没有定义。
Maybe it is defined but you have not posted its code here. 也许它已定义但您尚未在此处发布其代码。 But we can only give advice with what you have. 但我们只能提供你所拥有的建议。 Maybe it was defined in intnode.h in which case including it at the start of search.h would solve this particular issue. 也许它是在intnode.h中定义的,在这种情况下,在search.h的开头包含它可以解决这个特殊问题。

I have edited that code and I get no compilations errors. 我编辑了那段代码而没有编译错误。
The main change was defining a struct intnode . 主要的变化是定义了一个struct intnode
The way I have defined it is a guess. 我定义它的方式是猜测。 Which struct intnode you need is up to you, but you need one for sure. 你需要哪个struct intnode取决于你,但你肯定需要一个。
Also have changed return type for search to void* . 还将search返回类型更改为void* And removed the * operator for counter from these kind of lines : 并从这些行中删除了*运算符for counter

search((*root)->R, lookingfor, *counter);

The fixed code. 固定代码。 search.h : search.h:

struct intnode
{
  void* key;
  struct intnode** R;
  struct intnode** L;
};

void* search(struct intnode** root, int lookingfor, int* counter);

search.c : 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM