If been trying to use the STL list with my own class, to make up nodes.
In other words, what I am trying to do is that instead of just pushing in just an integer or a character for example, I want to be able to push in my own object, for example a coordinate with two int variables in it. Here is the code:
This is the node:
Code:
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
public:
int yvalue;
int zvalue;
Coordinate( int, int );
};
Coordinate::Coordinate( int value1, int value2 )
{
yvalue = value1;
zvalue = value2;
}
#endif
And now my main:
Code:
#include <iostream>
#include <list>
#include "coordinate.h"
using namespace std;
int main()
{
list< Coordinate > myGrid;
list< Coordinate >::iterator iter;
Coordinate *newPtr1 = new Coordinate( 0, 1 );
Coordinate *newPtr2 = new Coordinate( 0, 2 );
Coordinate *newPtr3 = new Coordinate( 1, 1 );
myGrid.push_back( newPtr1 );
myGrid.push_back( newPtr2 );
myGrid.push_back( newPtr3 );
iter = myGrid.begin();
}
My question is: how do I reference each individual member of the coordinate. Can I do just [cout << *iter->yvalue;] ?
Or, do I need to create the necesary functions inside the coordinate class to return the desired data?
So far every example and tutorial that I have seen deals only with lists and simple data types like ints and strings. But none that I have seen with user defined types. If I could use the stl list it would facilitate so much for me.
Please help?
Thanks,
Figa