I’m having a problem with the new ranged based for statement:
for ( InputReaderObject irb : InputReader )
{
......
}
So as you can see, i have a class called InputReaderObject, and then another variable that is defined as an extern in a included header file. And is declared long before used here as an InputReaderObject. Now, to the actual problem, both my intellisence and my compiler is strangling me
no callable ‘begin’ function found for type ‘InputReaderObject *’.
Now my suspicous is that in the included header file, it is declared as an extern, and isn’t really visible as an array of InputReaderObject(it is declared as an array).
So, does ranged based for statements work with pointers? Or is this my problem?
Thank You
If
InputReaderis declared in the header file as an array of constant size, then it should work as long as you#include <array>or#include <iterator>or any of several other library headers, so that you get the declarationsBut what if
InputReaderis declared something likeor maybe
…? It’s not obvious from that how many objects are in the array, but let’s suppose the same header also has something like:
Anything you pass into the range-based for loop needs to have
beginandendfunctions which return iterators. (The functions can be class members, found by argument-dependent lookup, or innamespace std.) The standard library defines somebeginandendfunctions for actual arrays and standard containers, but you can extend it by defining your own:would let you do…
(Making
irba reference instead of a copy is just a suggestion. You might actually need a copy for some reason.)You could also define
InputReaderObject* begin(InputReaderObject*);andInputReaderObject* end(InputReaderObject*);if you really want the loop to work withInputReaderas already declared. But that could cause confusing results if somebody had a pointer of that type which is not in factInputReader.