Search code examples
c++templatestinyxml

TinyXML and fetching values


I'm trying to load data from xml-file with TinyXML (c++).

int height = rootElem->attrib<int>("height", 480);

rootElem is a root element of loaded xml-file. I want to load height value from it (integer). But I have a wrapper function for this stuff:

template<typename T>
T getValue(const string &key, const string &defaultValue = "")
{
    return mRootElement->attrib<T>(key, defaultValue);
}

It works with string:

std::string temp = getValue<std::string>("width");

And it fails during fetching:

int temp = getValue<int>("width");


>no matching function for call to ‘TiXmlElement::attrib(const std::string&, const std::string&)’

UPD: The new version of code:

template<typename T>
T getValue(const string &key, const T &defaultValue = T())
{
    return mRootElement->attrib<T>(key, defaultValue);
}

Solution

  • The reason is you are calling the int version of TiXmlElement::attrib, but you are giving it a defualtValue of type const std::string &, however, the function expects a defaultValue of type int.