Getter and setter skins for TiXmlElement *

I am rewriting the project to use getters and setters to reference the TiXmlElement * However, I quickly run into problems that seem to be related to debug mode:

Ecxerpt from my class header:

TiXmlElement *_rootElement;
TiXmlElement *_dialogsElement;
TiXmlElement *_dialogElement;
TiXmlDocument _document;
void setDocument (TiXmlDocument doc) { this->_document = doc; }
void setRootElement (TiXmlElement * element) { this->_rootElement = element; }
void setDialogsElement (TiXmlElement * element) { this->_dialogsElement = element; }

TiXmlDocument getDocument () { return this->_document; }
TiXmlElement* getRootElement () { return this->_rootElement; }
TiXmlElement* getDialogsElement () { return this->_dialogsElement; }

      

Excerpt from the class constructor:

DCXML::DCXML(const char *dialogMark,const char *dialogName,TiXmlDocument doc) {
...
this->setDocument(doc);
this->setRootElement(this->getDocument().FirstChildElement("dcxml"));
this->setDialogsElement(this->getRootElement()->FirstChildElement("dialogs"));

      

Excerpt from class instance:

TiXmlDocument doc(input.gettok(2,"\"").to_chr());
bool dcxmlFile = doc.LoadFile();
...
DCXML *dcxml = new DCXML(input.gettok(2).to_chr(),input.gettok(3).to_chr(),doc);

      

Now for the weird part. This works until

this->setDialogsElement(this->getRootElement()->FirstChildElement("dialogs"));

      

in the constructor.

-> FirstChildElement ("dialogs") throws the error "CXX0039: error: character ambiguous" in VS2008 in debug mode.

The weird part is IntelliSense picks the FirstChildElement method and the compiler doesn't throw any errors.

What's even weirder is when in release mode it just fails to get the dialogs element.

What am I doing wrong? Or if you have successfully wrapped the getter for TiXmlElement *, let me know how I can !!

For a complete reference, here's an excerpt from the XML file:

<?xml version="1.0" encoding="utf-8"?>
<dcxml>
    <dialogs>
        <dialog 
            name="mediaplayer" 
            center="" 
            w="300" 
            h="400" 
            caption="Mamp 4.0 BETA" 
            border="btmnzy">
        </dialog>
    </dialogs>
</dcxml>

      

Feedback would be much appreciated as I am at a dead end :)

+1


source to share


1 answer


Make sure that

TiXmlDocument getDocument () { return this->_document; } 

      

Will not deep copy the TiXmlElement it contains. Otherwise you are returning a temporary value, use in the constructor to set the root node, which will then be destroyed. I haven't looked into his API, but I just know about such pitfalls.

The reason for the ambiguous call is as follows:

There are three overloads FirstChildElement

with one argument:

const TiXmlElement *    FirstChildElement (const char *value) const // :1
const TiXmlElement *    FirstChildElement (const std::string &_value) const // :2
TiXmlElement       *    FirstChildElement (const std::string &_value) // :3

      

You access the TiXmlElement via TiXmlElement&

(using a pointer TiXmlElement*

). But version c const char*

has an implicit object parameter TiXmlElement const&

. That is, a qualification transformation is required to transform the call. Other versions with std::string const&

also require conversions:



<implied obj param> <implicit obj param>    <arg1>         <param1>
TiXmlElement&        TiXmlElement const&    char const*    char const*         // :1
TiXmlElement&        TiXmlElement const&    char const*    std::string const&  // :2
TiXmlElement&        TiXmlElement&          char const*    std::string const&  // :3

      

There is ambiguity between the first and third overload. The easy decision is to

this->setDialogsElement(
    this->getRootElement()->FirstChildElement(std::string("dialogs")));

      

The latest version will be called instead. Another fix is ​​const_cast:

this->setDialogsElement(
    const_cast<TiXmlElement const*>(this->getRootElement())->
        FirstChildElement("dialogs"));

      

which is called by the first version. As for why this only happens in DEBUG ... I remember that TiXML has the ability to disable the use of STL. Maybe in release mode you disabled it (and thus overloads taking over std::string

), but in debug mode you forgot?

+2


source







All Articles