字段'value'具有不完整的类型

时间:2015-05-08 14:22:16

标签: c++ class compilation

我有一个C ++相互依赖的问题,我无法理解问题出在哪里......

以下是我的标题:

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;
...
}

当我尝试编译我的代码时,我有这个错误:

  

elem_info.h:10:15:错误:字段'value'的类型不完整        JSON_TYPE值;

看起来它无法找到JSON_TYPE。我无法理解问题出在哪里。

3 个答案:

答案 0 :(得分:3)

你在这里有前瞻声明

class JSON_TYPE;

class elem_info {
public:
    std::string key;
    JSON_TYPE value;
...
}; 

valueJSON_TYPE的一个实例。如果您的成员是指针或引用,则只能转发声明,而不是实际实例。

事实上,由于你在前方声明之前有一个完整的包含,你根本不需要前向声明,正如我所说,无论如何它都无济于事。你没问题:

#ifndef __ELEM_INFO__
#define __ELEM_INFO__

#include "json.type.h"

class elem_info {
public:
    std::string key;
    JSON_TYPE value;
...
}; 

#endif

答案 1 :(得分:1)

你无法做到:

class JSON_TYPE;

class elem_info {
public:
    std::string key;
    JSON_TYPE value;
...
}; 

JSON_TYPE是一种不完整的类型。你可以有一个指针或引用但没有实际的实例,因为编译器不知道它是什么。

答案 2 :(得分:1)

json.type.h包括json.array.h,其中包含json.object.h,其中包含json.type.h

那无能为力。

相关问题