【问题标题】:C++: Declare, define and call function which returns object of template class?C ++:声明,定义和调用返回模板类对象的函数?
【发布时间】:2015-04-08 13:34:01
【问题描述】:

我正在使用模板在 C++ 中实现一个模态类,该类存储任何类型的值。所有实现都在同一个文件中。这个模态类CConfigProperty(模板类)有两个变量值DefaultValue和它存储的值类型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

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    很遗憾,您将无法使用

    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&lt;int&gt;CConfigProperty&lt;std::string&gt; 是完全不同的类型,所以您将无法在一个 std::map 中混合使用这两种类型。

    您正在做的是编译时多态性,而此时您需要运行时多态性
    您需要添加一个非模板化的基类,并从中派生CConfigProperty&lt;&gt;,这样您就可以使用map 中的基类指针。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      相关资源
      最近更新 更多