0

So far, the range operator has worked on any Qt List type I used, except QDomNodeList. Since the range operator is defined as: "Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container." i can't see any reason why...

QDomNodeList domNodes = doc.elementsByTagName("peripheral");

for (QDomNode node : domNodes)
{
    qDebug() << node.nodeName() << "test";
}

...won't compile but the plain old for loop will:

QDomNodeList domNodes = doc.elementsByTagName("peripheral");

for (int i = 0; i < domNodes.size(); ++i)
{
    QDomNode node = domNodes.at(i);
    qDebug() << node.nodeName() << "test";
}

Compiler output seems rather obvious:

error: 'begin' was not declared in this scope
     for (QDomNode node : domNodes)
...
note:   'std::begin'
     begin(_Tp (&__arr)[_Nm])
     ^
                          ^

Plus the same error for std::end.

tobilocker
  • 891
  • 8
  • 27
  • 1
    Possible duplicate of [How do I use foreach with QDomNodeList in Qt?](http://stackoverflow.com/questions/2914631/how-do-i-use-foreach-with-qdomnodelist-in-qt) – acraig5075 May 03 '17 at 12:36
  • Similar question, true. But it is based on Qt's own `foreach` keyword and not the C++ range operator. Probably the answer fits both questions. The answer provides a link to `container classes` which is outdated and so far i did not find anything in Qt's reference about `QDomNodeList` not being a container type. – tobilocker May 03 '17 at 12:38
  • QDomNodeList doesn't provide iterators which the C++ range based for loop needs. Someone should implement that (and someone has, here : http://silmor.de/qtstuff.domfor.php) – nos May 03 '17 at 12:41
  • 1
    Both `foreach` and range-based for loop require the structure to implement a `begin` and `end` which QDomNodeList does not. That is why you can't do so. – acraig5075 May 03 '17 at 12:41
  • Alright. In both cases one probably has to accept the missing iterators. Since this is not a new problem i assume that this is missing for a reason (which i don't see). Not really satisfied with the answer to the other question but rather with the comment given by @acraig5075 – tobilocker May 03 '17 at 12:48
  • @tobilocker Retracted the close vote because I agree those answers don't help you. – acraig5075 May 03 '17 at 12:55

0 Answers0