【发布时间】: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
【问题讨论】: