2013-08-21 113 views
0

我有这样的代码:没有匹配函数调用

class XMLNode 
    { 
     //... 
     template <typename T> 
     bool getValue(T& t, const std::string& path) const 
     { 
      if (empty()) 
      { 
       throw std::runtime_error("Empty node"); 
      } 
      return nsXML::getValue(t, path, *node); 
     } 

     template <typename T> 
     T getValue(const std::string& path) const 
     { 
      if (empty()) 
      { 
       throw std::runtime_error("Empty node"); 
      } 
      return nsXML::getValue<T>(path, *node); 
     } 
     //... 
    }; 

class XMLData 
{ 
    //... 
    template <typename T> 
    T getValue(const std::string& path) 
    { 
     return XMLNode(&mDocNode, 0).getValue(path); // ERROR LINE 
    } 
    //... 
}; 

,给我错误

no matching function for call to ‘nsXML::XMLNode::getValue(const string&)’ 
note: candidates are: 
note: template<class T> bool nsXML::XMLNode::getValue(T&, const string&) const 
note: template<class T> T nsXML::XMLNode::getValue(const string&) const 

为什么g++给我这个错误?

+2

你需要传递一些模板参数该成员函数调用:'的XMLNode(mDocNode, 0).getValue (path);' – juanchopanza

回答

1

编译器无法评估您想要实例化函数模板的类型。在这种情况下,你必须明确地指定它:

return XMLNode(&mDocNode, 0).getValue<T>(path); 
            // ^-- explicit instantiation 

只有在某些情况下,模板参数可以通过编译器自动从函数参数推断:

int i; 
bool b = XMLNode(&mDocNode, 0).getValue(i, path); 

这里,编译器看到一个int作为第一个函数参数,并可以推断T代表该函数调用为int,所以它的同

bool b = XMLNode(&mDocNode, 0).getValue<int>(i, path); 
0

因为你的XMLNode :: getValue(const std :: string & path)函数是一个const,所以当它调用nsXML :: getValue时,它正在寻找const版本,我想没有定义一个const 。

注意一个const成员函数和一个非const成员函数是不同的。