PDA

View Full Version : recursive problem



Shawn
16th October 2007, 11:22
I used DOM to parse an XML file


QDomNode node = root.firstChild();
if (!node.isNull()) {
node = findTag(node, "Communication");
//do something with node
}

QDomNode DomParser::findTag(QDomNode &node, const QString &tag)
{
if (node.toElement().tagName() == tag) {
return node;
}else {
if (hasChild(node)) {
node = node.firstChild();
findTag(node, tag);
}else {
while (!hasSibling(node)) {
if (hasParent(node))
node = node.parentNode();
else
QMessageBox::information(0, "Error", "Can not find Tag");
}
node = node.nextSibling();
findTag(node, tag);
}
}
}

inline bool DomParser::hasParent(const QDomNode &node)
{
if( !node.parentNode().isNull())
return true;
else
return false;
}

inline bool DomParser::hasSibling(const QDomNode &node)
{
if( !node.nextSibling().isNull())
return true;
else
return false;
}

inline bool DomParser::hasChild(const QDomNode &node)
{
return node.hasChildNodes();
}

There is always an error in "qatomic_windows.h"

inline int q_atomic_decrement(volatile int *ptr)
{ return _InterlockedDecrement(reinterpret_cast<volatile long *>(ptr)); }

Is there anything wrong in my code ?

marcel
16th October 2007, 11:28
Can you be more specific about the error?
And your function will not work. Instead of just calling findTag inside findTag, use return findTag().

Shawn
16th October 2007, 11:36
Can you be more specific about the error?
And your function will not work. Instead of just calling findTag inside findTag, use return findTag().

in function findTag();
firstly it will go throught this piece of code:

if (node.toElement().tagName() == tag) {
return node;
}
and return if the statement is ture

The problem is that I can not find the specific problem.

Any hint will be appreciated!

marcel
16th October 2007, 11:39
Yes, let's suppose you are on the second level of recursion and return the node. The function will return in one of the two places it is called on the "else" branch. Now, who's gonna return your node from there? The function just exits and it shouldn't even compile since not all control paths return something.

Shawn
16th October 2007, 11:55
Yes, let's suppose you are on the second level of recursion and return the node. The function will return in one of the two places it is called on the "else" branch. Now, who's gonna return your node from there? The function just exits and it shouldn't even compile since not all control paths return something.

Stupid mistake.
I should use return findTag() in the two else branchs instead of just call findTag(), as you suggested above.

Thank you very much marcel.:)

marcel
16th October 2007, 11:57
You're welcome!

ToddAtWSU
16th October 2007, 18:54
The function just exits and it shouldn't even compile since not all control paths return something.

Unfortunately some compilers might issue a warning or nothing at all, as long as they see a return statement. I have run into that situation before and it takes a while to realize the one thing you're not doing is returning anything because of a conditional.