views:

49

answers:

2

Hello, 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);
}
+1  A: 

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.

iconiK
+1  A: 

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