2015-05-08 51 views
0

我有一个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: error: field ‘value’ has incomplete type JSON_TYPE value;

它看起来像它不能找到JSON_TYPE。我不知道问题在哪里。

回答

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 
+0

是的,好点。谢谢。 –

+1

除非不是因为他有循环包含。 –

1

你不能做:

class JSON_TYPE; 

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

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

+0

感谢您的快速回答。 –

+1

不正确。他在紧接[冗余]前向声明之前包含了全部定义。 -1 –

+0

@LightnessRacesinOrbit我刚刚注意到有比我放的更多的问题。这应该只是删除? – NathanOliver

1

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

这是行不通的。

+0

它在ELEM_INFO中使用指向TYPE的指针。 –

+0

@约翰:那么这不是你真正的代码。 –