【问题标题】:Type does not name a type or error: field has incomplete type类型未命名类型或错误:字段类型不完整
【发布时间】:2012-05-20 07:47:24
【问题描述】:

我不确定为什么会收到此错误...“错误:字段 'config' 的类型不完整”。我尝试使用#include 进行前向声明并包含标头...我只是想在 fInstance 中包含 fConfig...

#ifndef FINSTANCE_H
#define FINSTANCE_H

//#include "fConfig.h"
#include <vector>
#include <sys/types.h>
#include <string>

using namespace std;

class fConfig;

class fInstance
{
public:
    pid_t pid;
    fConfig config;
    vector<string> notes;
    vector<time_t> times;

    fInstance();
    virtual ~fInstance();
};

#endif // FINSTANCE_H





#ifndef FCONFIG_H
#define FCONFIG_H

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <fcntl.h>
#include "JWDSFork.h"
#include "fInstance.h"

using namespace std;

class fConfig
{
    private:
        pid_t pid, w;

    public:
        pid_t cPid;
        string name;

        int group;
        int instanceId;
        int numInstance;
        int tries;

        bool reply;
        bool debug;
        bool service;
        bool currentlyRunning;

        time_t startTime;
        time_t endTime;

        string path;

        fConfig();
        virtual ~fConfig();

        void start();
        string intToString(int);
        char* stringToChar(string);
};

#endif // FCONFIG_H

编辑:添加更改

#ifndef FINSTANCE_H
#define FINSTANCE_H

#include "fConfig.h"
#include <vector>
#include <sys/types.h>
#include <string>
using namespace std;

//class fConfig;

class fInstance
{
public:
    pid_t pid;
    fConfig* config;
    vector<string> notes;
    vector<time_t> times;

    fInstance();
    virtual ~fInstance();
};

#endif // FINSTANCE_H




#ifndef FCONFIG_H
#define FCONFIG_H

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <fcntl.h>
#include "JWDSFork.h"
//#include "fInstance.h"

using namespace std;

class fConfig
{
    private:
        pid_t pid, w;

    public:
        pid_t cPid;
        string name;

        int group;
        int instanceId;
        int numInstance;
        int tries;

        bool reply;
        bool debug;
        bool service;
        bool currentlyRunning;

        time_t startTime;
        time_t endTime;

        string path;

        fConfig();
        virtual ~fConfig();

        void start();
        string intToString(int);
        char* stringToChar(string);
};

#endif // FCONFIG_H

【问题讨论】:

    标签: c++ forward-declaration incomplete-type


    【解决方案1】:

    您需要在fInstance.h 中添加#include "fConfig.h",并且您不需要在fConfig.h 中添加fInstance.h,因为您似乎没有在fConfig.h 中使用fInstance 类型。

    当您转发声明类型时,编译器不知道该类型的内存布局,因此它会将类型视为不完整类型,并且编译器无法执行任何需要知道的操作该类型的内存布局。

    您在转发声明后创建fConfig 的实例,为此编译器需要知道fConfig 的内存布局,但由于您只是转发声明它,它不知道这一点并因此抱怨。

    【讨论】:

    • @op:当您只使用指向该数据类型的引用或指针时,转发声明很有用。由于您使用的是完整实例,因此您需要完整的定义。
    • @Als 所以创建实例是使用新的?我以为调用 fConfig config 声明了一个实例?
    • @Spidey 我如何完全声明这个实例?
    • @JonH:它向类声明了一个成员,但编译器需要估计在fInstance 中存储fConfig 类型的对象所需的内存量。编译器知道fInstance 对象需要多少内存?通过总结fInstance 的所有成员所需的内存,所以..
    • @Als 所以我应该做点什么fConfig * config;????如果是这样,我如何在 cpp 中访问该类的成员?
    【解决方案2】:

    您需要将fConfig 的完整定义放在fInstance 之前。前向声明不起作用,因为您的 fInstance 有一个 fConfig 数据成员,因此需要完整类型:

    fConfig.h:

    #ifndef FCONFIG_H
    #define FCONFIG_H
    
    class fConfig {
      // as in original
    };
    
    #endif
    

    fInstance.h:

    #ifndef FINSTANCE_H
    #define FINSTANCE_H
    
    #include "fConfig.h" // need to include to get full type
    
    class fInstance {
      // as in original
    };
    
    #endif
    

    【讨论】:

    • 好的,完整的定义是什么意思...我需要使用新的吗?
    • @JonH 不,一点也不。只需将所有fConfig 内容放在fInstance 之前,然后从fConfig.h 中删除fInstance.h 包含。
    • 我声明它超出了课程的范围吗?
    • 当我这样做时,它说 fConfig 没有命名类型
    • @JonH 你一定做错了什么。我建议的编译,见ideone.com/0ndpx
    猜你喜欢
    • 2012-09-10
    • 2011-04-29
    • 1970-01-01
    • 2023-04-10
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-01-28
    • 2018-05-11
    相关资源
    最近更新 更多