im creating a function where i need two different structures that are in different files, so i tried to include the files inside each other and it work for the first function, but the others give me this error
Error: unknown type name 'Activities'
'Activities' being a struct type in the file
file companies.h where the error is
#ifndef COMPANIES_H
#define COMPANIES_H
#ifdef __cplusplus
extern "C" {
#endif
#include "activities.h"
#define COMPANY_INITIAL_SIZE 20
#define COMENT_INITIAL_SIZE 2
#define SEARCHES_INITIAL_SIZE 20
#define RATING_INITIAL_SIZE 2
#define TEXT_MAX_SIZE 200
#define NAME_MAX_SIZE 50
#define EMAIL_MAX_SIZE 50
#define TITLE_MAX_SIZE 50
#define ADDRESS_MAX_SIZE 70
#define LOCATION_MAX_SIZE 70
#define CATEGORY_MAX_SIZE 30
#define NIF_MIN_SIZE 100000000
#define NIF_MAX_SIZE 999999999
enum category {
microCompany = 1, pmeCompany, macroCompany
};
enum state {
inactive, active
};
enum searchCamp {
nif, businessArea, nameLocation
};
enum comentState {
hidden, visible
};
typedef struct {
char title[TITLE_MAX_SIZE];
char text[TEXT_MAX_SIZE];
char userName[NAME_MAX_SIZE];
char userEmail[EMAIL_MAX_SIZE];
enum comentState states;
} Coment;
typedef struct {
int nota;
char userName[NAME_MAX_SIZE];
char userEmail[EMAIL_MAX_SIZE];
} Rating;
typedef struct {
int Nif;
char name[NAME_MAX_SIZE];
enum category categories;
int businessAreaCode;
enum state states;
char address[ADDRESS_MAX_SIZE];
char location[LOCATION_MAX_SIZE];
int postalCode;
int comentCounter, comentSize;
Coment *coments;
int ratingCounter, ratingSize;
Rating *ratings;
} Company;
typedef struct {
int counter, size;
Company *companies;
} Companies;
void freeCompanies(Companies *);
void loadCompanyFile(Companies *, char*);
void saveCompanyFile(Companies *, char *);
void insertCompanies(Companies *, Activities *); //<- line that gives the error
void insertComents(Companies *);
void insertRatings(Companies *);
void expandCompanySize(Companies *);
void searchNifCompanies(Companies);
void printCompany(Companies, int);
void editCompanies(Companies *);
char* getStringCategory(Companies *, int);
char* getStringState(Companies *, int);
void searchNameCompanies(Companies);
void removeCompanies(Companies *);
void removeComents(Companies *);
void AlterComentState(Companies *);
#ifdef __cplusplus
}
#endif
file activities.h where it worked:
#ifndef ACTIVITIES_H
#define ACTIVITIES_H
#include "companies.h"
#ifdef __cplusplus
extern "C" {
#endif
#define ACTIVITIES_INITIAL_SIZE 20
typedef struct {
int activityCode;
char name[NAME_MAX_SIZE];
enum state states;
} Activity;
typedef struct {
int activityCounter, size;
Activity *activities;
} Activities;
void loadActivityFile(Activities*,char*);
void saveActivityFile(Activities*,char*);
void insertActivities(Activities*);
void removeActivities(Activities*, Companies*);
void editActivities(Activities*);
void changeActivityState(Activities*);
int verifyActivityUse(Companies*, Activities*, int);
#ifdef __cplusplus
}
#endif
#endif /* ACTIVITIES_H */
Type definitions and typedef declarations at file scope are visible from the point at which they appear to the end of the file, excluding any places where they are shadowed by a different definition of the same identifier.
In your case, it doesn't matter which of the two headers is included first. Either way, a function declaration that depends on one of the two main types will appear at a point before the definition of that type.
The most general way to solve circularity problems like this is with forward (struct
) declarations, but that's actually a problem for you because you're using untagged structure types. You cannot forward declare an untagged structure type because there is no way to complete that type later, though you could enable that by adding tags.
In your particular case, however, it looks like you can also resolve the circularity by factoring the type definitions into their own header, separate from the function declarations. Each of the current headers then #include
s the one with the type definitions instead of them #include
ing each other.