无效使用不完整类型'class ...'

时间:2017-10-07 19:57:40

标签: c++ class pointers inheritance

我一直在设置一个设置类,如果此文件不存在,可以从文件加载设置或加载默认设置。 我的想法是有一个CSettings类来处理所有设置和文件查询。还有一个CDefaultSettings类,它存储所有默认设置。 这两个类都必须能够访问枚举集。

我得到的错误是:

Settings.cpp: In member function 'std::string CSettings::GetSetting(const CSettings::TProgramSetting&)':
Settings.cpp:6:28: error: invalid use of incomplete type 'class CDefaultSettings'
     return iDefaultSettings->GetDefaultSetting(aProgramSetting);
                            ^

谁能告诉我我做错了什么

我的档案:

的main.cpp

#include <iostream>
#include <string>
#include "Settings.h"

int main()
{
    CSettings programSettings;
    std::string setting = programSettings.GetSetting(CSettings::TProgramSetting::Setting1);
    std::cout << setting << std::endl;

    return 0;
}

Settings.h

#include <map>
#include <string>

class CDefaultSettings;

class CSettings
{
    public:
        typedef enum {
            Setting1,
            Setting2,
            Setting3
        } TProgramSetting;

    private:
        CDefaultSettings *iDefaultSettings;

    public:
        std::string GetSetting(const CSettings::TProgramSetting& aProgramSetting);
};

Settings.cpp

#include "Settings.h"

std::string CSettings::GetSetting(const CSettings::TProgramSetting& aProgramSetting)
{
    // As a test, return a default setting
    return iDefaultSettings->GetDefaultSetting(aProgramSetting);
}

DefaultSettings.h

#include <map>
#include "Settings.h"

class CDefaultSettings
{
    public:
        typedef std::map<CSettings::TProgramSetting, std::string> TDefaultProgramSettingCollection;
        TDefaultProgramSettingCollection iDefaultProgramSettingCollection;

    public:
        void LoadDefaultSettings();
        std::string GetDefaultSetting(const CSettings::TProgramSetting& aProgramSetting);
};

DefaultSettings.cpp

#include "DefaultSettings.h"

void CDefaultSettings::LoadDefaultSettings()
{
    iDefaultProgramSettingCollection[CSettings::TProgramSetting::Setting1] = "Default setting 1";
    iDefaultProgramSettingCollection[CSettings::TProgramSetting::Setting2] = "Default setting 1";
    iDefaultProgramSettingCollection[CSettings::TProgramSetting::Setting3] = "Default setting 1";
}

std::string CDefaultSettings::GetDefaultSetting(const CSettings::TProgramSetting& aProgramSetting)
{
    return iDefaultProgramSettingCollection[aProgramSetting];
}

1 个答案:

答案 0 :(得分:3)

您的Settings.cpp文件似乎缺少#include "DefaultSettings.h"

另外一定要添加包含警卫la

#ifndef SETTINGS_H 
#define SETTINGS_H
...
#endif`

进入settings.h(通常在每个标题中),否则它将包含在#include "Settings.h"中的settings.cppDefaultSettings.h中包含的重复定义中。