Reputation: 37
I have a C++ interdependence problem and i can not understand where the problem is...
Here are my headers:
json.array.h
#ifndef __JSON_ARRAY__
#define __JSON_ARRAY__
#include "json.object.h"
class JSON_OBJECT;
/* JSON_ARRAY */
class JSON_ARRAY {
int size;
custom_list<JSON_OBJECT> * container;
...
};
#endif
json.object.h
#ifndef __JSON_OBJECT__
#define __JSON_OBJECT__
#include "hash.h"
#include "elem_info.h"
#include "json.type.h"
class JSON_TYPE;
class elem_info;
/* JSON_OBJECT */
class JSON_OBJECT {
custom_list<elem_info> *H;
int HMAX;
unsigned int (*hash) (std::string);
...
};
#endif
json.type.h
#ifndef __JSON_TYPE__
#define __JSON_TYPE__
#include "json.object.h"
#include "json.array.h"
class JSON_OBJECT;
class JSON_ARRAY;
class JSON_TYPE {
JSON_ARRAY * _JSON_ARRAY_;
JSON_OBJECT * _JSON_OBJECT_;
std::string _JSON_OTHER_;
std::string _JSON_TYPE_;
...
};
#endif
elem_info.h
#ifndef __ELEM_INFO__
#define __ELEM_INFO__
#include "json.type.h"
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
#endif
main.cpp
#include <iostream>
#include <string>
#include "custom_list.h" // it inculdes cpp also
#include "json.type.h"
#include "elem_info.h"
#include "json.object.h"
#include "json.array.h"
#include "json.type.cpp"
#include "elem_info.cpp"
#include "json.object.cpp"
#include "json.array.cpp"
int main()
{
JSON_ARRAY * root = new JSON_ARRAY;
JSON_OBJECT obj;
JSON_OBJECT obj1;
JSON_OBJECT * obj2 = new JSON_OBJECT;
JSON_TYPE * type = new JSON_TYPE;
...
}
When i try to compile my code, i have this error:
elem_info.h:10:15: error: field ‘value’ has incomplete type JSON_TYPE value;
It looks like it cant find JSON_TYPE. I cant understand where is the problem.
Upvotes: 0
Views: 1530
Reputation: 385274
json.type.h
includes json.array.h
which includes json.object.h
which includes json.type.h
.
That can't work.
Upvotes: 1
Reputation: 180935
You can't do:
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
JSON_TYPE
is an incomplete type. You can have a pointer or reference but no actual instance since the compiler does not know what it is.
Upvotes: 2
Reputation: 117926
You have a forward declaration here
class JSON_TYPE;
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
But value
is an instance of JSON_TYPE
. You can only forward declare if you have members that are pointers or references, not actual instances.
In fact, since you have a full include before that forward declaration, you don't need the forward declaration at all, and as I said it wouldn't help you anyway. You'd be fine with:
#ifndef __ELEM_INFO__
#define __ELEM_INFO__
#include "json.type.h"
class elem_info {
public:
std::string key;
JSON_TYPE value;
...
};
#endif
Upvotes: 3