[英]Templated member function call g++ error : no matching function for call
g++ encounter an error on that sample. g ++在该示例上遇到错误。
I have a class Option
who contain an std::string
我有一个包含
std::string
的类Option
OptionValue
inherited from Option
with an template type and a templated argument of type std::string
for the key. 从
Option
继承的OptionValue
,具有模板类型和密钥的std::string
类型的模板化参数。
OptionManager
manage OptionValue
in a std::map<std::string, Option*>
OptionManager
have a member function create
: OptionManager
在std::map<std::string, Option*>
管理OptionValue
, OptionManager
具有成员函数create
:
template <typename T, std::string & key>
void create(const T & value);
g++ don't complain if I don't invoke : 如果我不调用g ++,请不要抱怨:
OptionManager *manager = new OptionManager;
manager->create<int, "my_key">(3);
g++ don't like create
call, here is the error : no matching function for call to OptionManager::create(int)
g ++不喜欢
create
调用,这是错误: no matching function for call to OptionManager::create(int)
If somebody can help me by showing me the way, I thank him very much !!! 如果有人可以通过给我看路来帮助我,我非常感谢他! :)
:)
Here is the code : 这是代码:
Option.hpp Option.hpp
class Option
{
public:
Option(std::string & key) :
key_(key)
{}
virtual ~Option()
{}
protected:
std::string key_;
};
OptionValue.hpp OptionValue.hpp
template <typename T, std::string & key>
class OptionValue : public Option
{
public:
OptionValue<T, key>(T val) :
Option(key),
val_(val)
{}
virtual ~OptionValue()
{}
private:
T val_;
};
OptionManager.hpp OptionManager.hpp
class OptionManager
{
public:
OptionManager(){}
~OptionManager(){}
template <typename T, std::string & key>
void create(const T & value)
{
Option *tmp;
tmp = new OptionValue<T, key>(value);
this->list_.insert(t_pair(key, tmp));
}
private:
std::map<std::string, Option*> list_;
typedef std::map<std::string, Option*>::iterator t_iter;
typedef std::pair<std::string, Option*> t_pair;
}; };
main.cpp main.cpp
int main()
{
OptionManager *manager;
manager = new OptionManager;
manager->create<int, "my_key">(3);
return 0;
}
g++ error g ++错误
main.cpp: In function ‘int main()’:
main.cpp:8:35: error: no matching function for call to ‘OptionManager::create(int)’
main.cpp:8:35: note: candidate is:
OptionManager.hpp:14:12: note: template<class T, std::string& key> void OptionManager::create(const T&)
Your second template parameter is of type std::string &
. 您的第二个模板参数的类型为
std::string &
。 You cannot initialise this with a temporary object (as, in your case, the one created by converting a string literal to std::string
). 您不能使用一个临时对象(例如,通过将字符串文字转换为
std::string
创建的对象)初始化该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.