簡體   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