I would like to undestand a bit more about iterators and how to create them.
In the code example below, the compiler is not happy with the following line of code:
Code: Select all
Iterator<int> i { planets };
What is the proper what to create an iterator to go over a list of type Planet?
The entire code below:
Code: Select all
// define some domain specific data types
typedef int Coordinate;
typedef String Name;
// describe a Planet
typedef struct Planet
{
Name name;
Coordinate x, y;
} Planet;
// A function to print a planet's information
void showPlanet( Planet planet )
{
printf("%s: %d, %d\n", planet.name, planet.x, planet.y);
}
// The Main app
class PlanetApp : Application
{
void Main()
{
// Some instantiations of
Planet earth = { "Earth", 3, 4};
Planet jupiter = { "Jupiter", 1, 1};
// An array of planets
Planet planets[2] =
{
{ "Mercury", 5, 5 },
{ "Pluto", 7, 7 }
};
showPlanet(earth); showPlanet(jupiter);
// An iterator to iterate through an array of planets.
Iterator<int> i { planets };
while(i.Next()) showPlanet(i);
}
}