Hello,
I'd like to thank everyone that helped me out last time when I was having class issues - I've taken your recommendations on board and rewritten my code. Here's an explanation of my problem :
I'm writing a simulation programme that incorporates a doubly-linked list as a way to control "pieces" on a "gameboard". I've now written a class to handle my nodes, and a class to handle the list of nodes itself.
My class Node is fairly basic, and manages what a node should contain. The code is as follows :
Code:
class Node
{
public:
Node *next;
Node *prev;
Node(); // CONSTRUCTOR
void move();
void place();
int readx();
int ready();
int type;
protected:
int x;
int y;
};
// Constructor
Node::Node()
{
next = NULL;
prev = NULL;
place();
x = 1;
y = 1;
type = NODE; // NODE is an enum, at the moment NODE == 0
}
int Node::readx()
{
return x;
}
int Node::ready()
{
return y;
}
// Node Movement
void Node::move()
{
positions[x][y].presence[type] = FALSE; // Remove this piece from its current place on the board
switch((rand() % 8) + 1) // Generate a random direction
{
case 1:
x += 1;
break;
case 2:
x += 1;
y += 1;
break;
case 3:
y += 1;
break;
case 4:
y += 1;
x -= 1;
break;
case 5:
x -= 1;
break;
case 6:
x -= 1;
y -= 1;
break;
case 7:
y -= 1;
break;
case 8:
x +=1 ;
y -= 1;
break;
}
// Cyclic board - pieces "wrap around" the edges of the board
if(x < 1) x = BOARDMAX;
if(x > BOARDMAX) x = 1;
if(y < 1) y = BOARDMAX;
if(y > BOARDMAX) y = 1;
positions[x][y].presence[type] = TRUE; // Place this piece on the board
// DEBUG
cout << "Moved.\n";
// Used to count that the correct number of nodes are being moved at first
}
// Set initial, random position on board
void Node::place()
{
x = (rand() % BOARDMAX) + 1;
y = (rand() % BOARDMAX) + 1;
positions[x][y].presence[type] = TRUE;
}
To clarify certain aspects of that, namely the positions.presence, here's that structure :
Code:
struct contents // Possible "pieces"
{
bool presence[TOTAL_TYPES];
};
contents positions[BOARDMAX][BOARDMAX]; // Game position grid
with TOTAL_TYPES being the last of my ENUM ( just an int ).
I have a class, called Mortal, which derives from Node. It adds a boolean, like so :
Code:
class Mortal : public Node
{
public:
bool death;
Mortal();
};
// Derived classes don't inherit a constructor, but is executed anyway, so we need a constructor anyway :
// Constructor
Mortal::Mortal()
{
type = MORTAL;
death = FALSE;
}
My problem comes when I try to use a pointer to a node object, Node * conductor ( my iterator, essentially ) to access my boolean, "death".
Code:
if(mortalslist.conductor->death) mortalslist.remove();
where mortalslist is an object of type List, with Mortals as nodes.
My error is :
Code:
'class Node' has no member named 'death'
Is there a way around this ? I don't want to create a pointer of type Mortal, as this will break all of the polymorphism I've worked into the code, and will make future expandability very poor. Can it be typecast ? Is there another, better solution ?
Thanks very much once again,
Quentin