繁体   English   中英

C ++:声明,定义并调用返回模板类对象的函数?

[英]C++: Declare, define and call function which returns object of template class?

我正在使用模板在C ++中实现可存储任何类型值的模式类。所有实现都在同一文件中。 模态类CConfigProperty (模板类)具有两个变量,值DefaultValue和它存储DataType的值的DataType 在另一个类CDefaultConfig我有一个std :: map,它将存储此类的对象。 为此,我创建了一个返回模板类对象的方法。 我在此实现中遇到两个编译错误。我正在使用Xcode。

1栏位的类型不完整'CDefaultConfig :: DefaultValue'

2没有匹配的成员函数可调用'SetStringValueforModal'

我不确定如何从另一个函数返回模板类的对象。 还有如何使用在一类中声明的模板到另一类。

下面是示例源代码。

#include <iostream>
#include <map>

int main(int argc, const char * argv[])
{
    return 0;
}


typedef enum CONFIG_DATA_TYPE {

    TYPE_INT = 0,
    TYPE_STRING = 3,
}DataType;

template <class DefaultValue>
class CConfigProperty
{

public:
    CConfigProperty(DataType type,
                    DefaultValue configProperty
                    );
    CConfigProperty();
    ~CConfigProperty(void);
private:
    DataType    m_type;
    DefaultValue  m_configProperty;  /**/Field has incomplete type 'CDefaultConfig::DefaultValue'**
};

声明DefaultValue m_configProperty时,字段具有不完整的'CDefaultConfig :: DefaultValue类型;

template <class DefaultValue>
CConfigProperty<DefaultValue>::CConfigProperty(DataType type, DefaultValue configProperty)
:m_type(type),
m_configProperty(configProperty)
{

}

template <class DefaultValue>
CConfigProperty<DefaultValue>::CConfigProperty()
{

}

template <class DefaultValue>
CConfigProperty<DefaultValue>::~CConfigProperty(void)
{
}


class CDefaultConfig
{

public:
    CDefaultConfig();
    ~CDefaultConfig(void);
private:
    void PopulateDefaultConfigForAllKeys(void);
    void printText();

    template <class DefaultValue>
    CConfigProperty<DefaultValue> *SetStringValueforModal(std::string theValue);
};


CDefaultConfig::CDefaultConfig(void)
{
    PopulateDefaultConfigForAllKeys();
}

CDefaultConfig::~CDefaultConfig(void)
{
}

template <class DefaultValue>
CConfigProperty<DefaultValue> * CDefaultConfig::SetStringValueforModal(std::string theValue)
{

    CConfigProperty<std::string> *theConfigProperty = new  CConfigProperty<std::string>(TYPE_STRING,theValue);
    return theConfigProperty;
}

void CDefaultConfig::PopulateDefaultConfigForAllKeys(void)
{
    printText();
    std::map<std::string, CConfigProperty<class DefaultValue> *> Properties;

     Properties["Test"]=SetStringValueforModal("10"); //No matching member function for call to 'SetStringValueforModal

}

调用SetStringValueforModal时没有匹配的成员函数可用于调用'SetStringValueforModal

不幸的是,您将无法使用

std::map<std::string, CConfigProperty<class DefaultValue> *> Properties;

就像你似乎打算那样。

问题是您需要使用@Arcathor在其答案中写道的具体类型:

std::map<std::string, CConfigProperty<int> *> Properties;
std::map<std::string, CConfigProperty<std::string> *> Properties;

但是 ,由于CConfigProperty<int>是与CConfigProperty<std::string>完全不同的类型, CConfigProperty<std::string>从编译器的角度来看,您将无法使用混合在一个std::map这两种类型。

当需要运行时多 态时 ,您正在做的是编译时多
您需要添加一个非模板化的基类,并CConfigProperty<>派生CConfigProperty<> ,以便可以在map使用基类指针。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM