Crash when building XML QDomElement in a method (probably just my bad C++)



  • Using MSVC 2010, Win 7 (64), Qt 4.8.4. I'm pretty sure I just got some parameter types wrong, but I just fail to see which... ? I'd also be grateful for hints to better solutions of this problem (teaching a class how to write itself to an XML file).

    @#include <QtXML>

    class base {
    public:
    virtual QDomElement& Write(QDomDocument& doc) {
    QDomElement& de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    return de;
    }
    };

    class derived : public base {
    public:
    QDomElement& Write(QDomDocument& doc) {
    QDomElement& de = base::Write(doc);
    de.setAttribute("Attr2", 2);
    return de;
    }
    };

    int main(int argc, char *argv[])
    {
    QDomDocument doc("testtype");
    QDomElement root = doc.createElement("test");
    doc.appendChild(root);
    //#define crash 1
    #ifdef crash
    // IMO using these two classes should result in
    // the exact same calls as the positive case below
    base t1;
    derived t2;

    root.appendChild(t1.Write(doc));
    root.appendChild(t2.Write(doc));
    #else
    // This is the positive case - no classes involved
    QDomElement& de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    root.appendChild(de);
    de = doc.createElement("Element1");
    de.setAttribute("Attr1", "Value1");
    de.setAttribute("Attr2", 2);
    root.appendChild(de);
    #endif
    QFile of("test.xml");
    if (!of.open(QIODevice::WriteOnly))
    return 1;
    QTextStream ts(&of);
    ts << doc.toString();
    of.close();
    return 0;
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.