I came across a problem with a qt foreach and a template with multiple template parameters.
QVector<Node<T, U> > nodes;
...
[append some data]
...
foreach(const Node<T, U>& node, nodes) {
...
}
I'm getting this error:
error: use of undeclared identifier 'Q_FOREACH'
I guess that it is due to the ,
in the template because the Qt macro does not detect that it inside another template declaration. How can I fix that without using normal for loops or C++11?
If your compiler supports C++11, you could use
foreach(auto node, nodes) { ... }
or even
for(auto node: nodes) { ... }
Otherwise, you can force the preprocessor to ignore the comma in the template like this:
#define COMMA ,
foreach(const Node<T COMMA U>& node, nodes) { ... }
Or you can use a typedef
typedef Node<T, U> NodeTU;
foreach(const NodeTU& node, nodes) { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With