Results 1 to 7 of 7

Thread: recursive problem

  1. #1
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default recursive problem

    I used DOM to parse an XML file

    Qt Code:
    1. QDomNode node = root.firstChild();
    2. if (!node.isNull()) {
    3. node = findTag(node, "Communication");
    4. //do something with node
    5. }
    6.  
    7. QDomNode DomParser::findTag(QDomNode &node, const QString &tag)
    8. {
    9. if (node.toElement().tagName() == tag) {
    10. return node;
    11. }else {
    12. if (hasChild(node)) {
    13. node = node.firstChild();
    14. findTag(node, tag);
    15. }else {
    16. while (!hasSibling(node)) {
    17. if (hasParent(node))
    18. node = node.parentNode();
    19. else
    20. QMessageBox::information(0, "Error", "Can not find Tag");
    21. }
    22. node = node.nextSibling();
    23. findTag(node, tag);
    24. }
    25. }
    26. }
    27.  
    28. inline bool DomParser::hasParent(const QDomNode &node)
    29. {
    30. if( !node.parentNode().isNull())
    31. return true;
    32. else
    33. return false;
    34. }
    35.  
    36. inline bool DomParser::hasSibling(const QDomNode &node)
    37. {
    38. if( !node.nextSibling().isNull())
    39. return true;
    40. else
    41. return false;
    42. }
    43.  
    44. inline bool DomParser::hasChild(const QDomNode &node)
    45. {
    46. return node.hasChildNodes();
    47. }
    To copy to clipboard, switch view to plain text mode 

    There is always an error in "qatomic_windows.h"
    Qt Code:
    1. inline int q_atomic_decrement(volatile int *ptr)
    2. { return _InterlockedDecrement(reinterpret_cast<volatile long *>(ptr)); }
    To copy to clipboard, switch view to plain text mode 

    Is there anything wrong in my code ?
    Last edited by Shawn; 16th October 2007 at 11:27.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: recursive problem

    Can you be more specific about the error?
    And your function will not work. Instead of just calling findTag inside findTag, use return findTag().

  3. The following user says thank you to marcel for this useful post:

    Shawn (17th October 2007)

  4. #3
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recursive problem

    Quote Originally Posted by marcel View Post
    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:
    Qt Code:
    1. if (node.toElement().tagName() == tag) {
    2. return node;
    3. }
    To copy to clipboard, switch view to plain text mode 
    and return if the statement is ture

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

    Any hint will be appreciated!

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: recursive problem

    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.

  6. The following user says thank you to marcel for this useful post:

    Shawn (17th October 2007)

  7. #5
    Join Date
    May 2007
    Posts
    91
    Thanks
    60
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recursive problem

    Quote Originally Posted by marcel View Post
    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.

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: recursive problem

    You're welcome!

  9. #7
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: recursive problem

    Quote Originally Posted by marcel View Post
    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.

Similar Threads

  1. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  2. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.