c++ - 字段 ‘value’ 的类型不完整

标签 c++ class compilation

我有一个 C++ 相互依赖问题,我不明白问题出在哪里......

这是我的标题:

json.数组.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.对象.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

元素信息.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

主要.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。我不明白问题出在哪里。

最佳答案

你在这里有一个前向声明

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

关于c++ - 字段 ‘value’ 的类型不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126153/

相关文章:

c++ - 从目标文件生成dll文件

c++ - CUDA:同步全局内存读写与计算能力 1.1

c++ - 如何设计对包含不同类型的文件的访问?

java - java中抽象类适合什么场景使用?

C++ 管理对象

ios - 如何从 xcode 部署的应用程序反编译

c++ - 为我的 Visual Studio C++ 项目配置目标平台

java - 下面的 java 枚举类的主体是如何工作的?

c - 如何编译APRON的例子?

haskell - cabal:依赖于具有特定编译标志的包