简体   繁体   English

模板化的成员函数调用g ++错误:没有匹配的调用函数

[英]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 : OptionManagerstd::map<std::string, Option*>管理OptionValueOptionManager具有成员函数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.

相关问题 使用g ++“没有匹配功能可解决呼叫错误” - “No matching function for call error” with g++ g ++没有匹配的函数调用错误 - g++ no matching function call error g ++“没有匹配的函数调用”错误:构造函数或运算符=不匹配? - 'no matching function for call to' error with g++ : constructor or operator = not matching? wxwidgets // g ++编译器错误:没有匹配的函数可以调用&#39;operator new(..&#39; - wxwidgets // g++ Compiler error: no matching function for call to 'operator new(..' 从g ++中的辅助函数调用构造函数时,“没有匹配函数会导致调用错误” - “no matching function for call error” when calling constructor from helper function in g++ boost&g ++:没有匹配函数来调用&#39;current_path()&#39; - boost & g++: no matching function for call to 'current_path()' 这个函数调用是否会被g ++优化? - Will this function call be optimized by g++? 没有匹配的 function 来调用:错误:必须使用 '.*' 或 '-&gt;*' 来调用指向成员 function 在 'f (...)' 中的指针,例如 '(... -&gt;* f) (...)' - No matching function to call: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘f (…)’, e.g. ‘(… ->* f) (…)’ No Matching函数使用const参数调用模板化函数 - No Matching function call to templated function with const parameters g ++在* expected *错误消息中给了我奇怪的函数调用 - g++ gives me strange function call in an *expected* error message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM