Reputation: 1292
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
Upvotes: 0
Views: 472
Reputation: 72271
If InputReader
is 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 declarations
template<typename T, std::size_t N> T* std::begin(T (&array)[N]);
template<typename T, std::size_t N> T* std::end(T (&array)[N]);
But what if InputReader
is declared something like
extern InputReaderObject* InputReader;
or maybe
extern InputReaderObject InputReader[];
...? It's not obvious from that how many objects are in the array, but let's suppose the same header also has something like:
extern unsigned int InputReaderSize;
Anything you pass into the range-based for loop needs to have begin
and end
functions which return iterators. (The functions can be class members, found by argument-dependent lookup, or in namespace std
.) The standard library defines some begin
and end
functions for actual arrays and standard containers, but you can extend it by defining your own:
#include <iterator>
struct InputReaderLoopType {
InputReaderObject* begin() const { return InputReader; }
InputReaderObject* end() const { return InputReader + InputReaderSize; }
};
extern const InputReaderLoopType InputReaderLoop;
would let you do...
for ( InputReaderObject& irb : InputReaderLoop )
(Making irb
a 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*);
and InputReaderObject* end(InputReaderObject*);
if you really want the loop to work with InputReader
as already declared. But that could cause confusing results if somebody had a pointer of that type which is not in fact InputReader
.
Upvotes: 5