没有匹配的呼叫功能

时间:2013-08-21 12:22:13

标签: c++ gcc g++

我有这段代码:

    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 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

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

相关问题