uhm uh... char* ... cstrings ... the cstring library should help you! :D
I think there is no operator== for char* but there is a function that compares cstrings: strcmp (short for: string compare)
strcmp returns 0 if both strings are the same :)
see:
http://www.cplusplus.com/reference/cstring/strcmp/
Now to your loop...
+1 for using recursion! :DD
But you never return anything (except this) :o
Also:
findTag((*temp.front()).getName());
there are to many brackets :b
And findTag should be used like this: node.findTag(tag);
so you would like to search your children for that tag, right?
temp.front().findTag(tag);
is the way to go! :D
Now you could make it like this:
- when nothing is found you return a null-pointer (NULL) (or "nothing")
- when something is found you return the first node that matches the tag
Now, let's get to work! :D
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <cstring>
// ...
Element* Element::findTag(char* tag)
{
list<Element*> temp = children;
int s = temp.size();
if(strcmp(getName(), tag) == 0) // returns 0 if both are the same ;)
{
return this;
}
Element* node = NULL;
for(int i = 0; i < s; i++)
{
node = temp.front().findTag(tag); // search the first node for that Tag
if(node != NULL) // if a node is found return that node
return node;
temp.pop_front();
}
return NULL;
// return nullptr; -> use this only if you are familiar with c++11
}
|
Note: untested code!