0

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);
}
Max Frai
  • 61,946
  • 78
  • 197
  • 306

2 Answers2

1

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.

CMircea
  • 3,543
  • 2
  • 36
  • 58
1

attrib<T>(key, defaultValue) probably expect it's first argument to be of same type as it's second template argument.

In other words; T in mRootElement->attrib<T>(key, defaultValue) must be of the same type as defaultValue.

Viktor Sehr
  • 12,825
  • 5
  • 58
  • 90