Reputation: 53
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!
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;
}
Upvotes: 5
Views: 7826
Reputation: 3200
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;
}
Upvotes: 0
Reputation: 213711
#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
Upvotes: 0