Question

g++ encounter an error on that sample.

I have a class Option who contain an std::string

OptionValue inherited from Option with an template type and a templated argument of type std::string for the key.

OptionManager manage OptionValue in a std::map<std::string, Option*> OptionManager have a member function create :

  template <typename T, std::string & key>
  void  create(const T & value);

g++ don't complain if I don't invoke :

  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)

If somebody can help me by showing me the way, I thank him very much !!! :)

Here is the code :

Option.hpp

class                                   Option
{
public:
  Option(std::string & key) :
  key_(key)
  {}

  virtual ~Option()
  {}

 protected:
   std::string                           key_;
 };

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

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

int                             main()
{
  OptionManager                 *manager;

  manager = new OptionManager;
  manager->create<int, "my_key">(3);
  return 0;
}

g++ error

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&)
Was it helpful?

Solution

Your second template parameter is of type 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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top