Bubi
Bubi

Reputation: 27

How can I fix an forward declaration warning in a structure

I have several structures, and I keep getting a warning message. I've been trying for several hours to kick it out, but I can't. I would really appreciate the help.

This is the Student struct in the Student.h header:

   #ifndef __STUDENT_H__
   #define __STUDENT_H__
   
   #include "Teacher.h"
   #include "ClassRoom.h"
   struct Teacher;
   struct ClassRoom;

   typedef struct {
       char * name;
       struct ClassRoom *myClassRoom;
       struct Teacher *myTeachers[3];
   } Student;

   void setTeacherDynamic(struct Teacher *t, struct Teacher* tt);

and this is the Student.c source code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Teacher.h"
#include "Student.h"

struct Teacher;

void setTeacherDynamic(Teacher *t, Teacher* tt)
{
    strcpy(t->name, tt->name);

    t->myClass = NULL;

}

The warning message is:
enter image description here The problem is with the function setTeacherDynamic(), and I don't know how to fix it.

The Teacher struct:

enter image description here

and this is when I'm calling to the function, also in Student.c

enter image description here

Upvotes: 0

Views: 158

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

You have multiple problems:

The first is that since you have forward declarations in Student.h you don't need to include Teacher.h or ClassRoom.h. And in the source file Student.c you can remove the forward declaration you have.

The second problem is that you don't actually have a struct Teacher. You have a Teacher type-alias for an anonymous structure. You should declare the actual structure for the forward declarations to work:

typedef struct Teacher { ... } Teacher;

You should do these changes for all your source and header files.

Upvotes: 1

Related Questions