C error: Identifier <funtion_from_other_header> is undefined

2.1k Views Asked by At

I have absolutely no clue why I'm getting these errors. My code is something like this:

type.h

typedef struct {
  int* myInt;
} myType;

class.h

#ifndef myClass
#define myClass

#include "type.h"

void makeMyType( myType& t );
void deleteMyType ( myType& t );

#endif

class.C

#include "type.h"

makeMyType( myType& t ) { 
  t.myInt = (int*) malloc (sizeof(int));
  t.myInt = 0;
}
deleteMyType ( myType& t ) {
  free (t);
}

useType.C

#include "type.h"
#include "class.h"

int main {
  myType t;
  makeMyType (t);
  deleteMyType (t);
  return 0;
}

Then the compiler gives me the following:

error: identifier "makeMyType" is undefined
error: identifier "deleteMyType" is undefined

What could be causing this error? How can I fix it?

2

There are 2 best solutions below

0
On
void makeMyType( myType& t );
void deleteMyType ( myType& t );

These 2 function prototypes are declared in class.h and not in type.h and you are including type.h in your class.c file which is why you are seeing this issue. class.h should be included in class.c

0
On

In class.c include class.h instead of type.h and in useType.c include just class.h

type.h

typedef struct {
  int* myInt;
} myType;

class.h

#ifndef myClass
#define myClass

#include "type.h"

void makeMyType( myType& t );
void deleteMyType ( myType& t );

#endif

class.C

#include "class.h"

makeMyType( myType& t ) { 
  t.myInt = (int*) malloc (sizeof(int));
  t.myInt = 0;
}
deleteMyType ( myType& t ) {
  free (t);
}

useType.C

#include "class.h"

int main {
  myType t;
  makeMyType (t);
  deleteMyType (t);
  return 0;
}