简体   繁体   中英

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. 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

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

This is my .c file

#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.

It is a good habit to include all needed header files needed for your .h + c code module from the header file.

Also use header guards in every h file:

#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.
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.

I have edited that code and I get no compilations errors.
The main change was defining a 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.
Also have changed return type for search to void* . And removed the * operator for counter from these kind of lines :

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

The fixed code. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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