I am looking to find the best way of creating a dynamic 2D Array of abstract/virtual class-objects.
I have one class called Prey which inherits from the Organism class.
Both classes have a move() function, although I understand that if I get the polymorphism to work properly the Organism move() should never be seen.
Long story short, I have a dynamic 2D array of "organisms" and I hope to cycle through and call the move function, and get the move functions of the Prey to execute.
Organsm.h
//
// This class is abstract
#ifndef _ORGANISM_H
#define _ORGANISM_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "WorldMap.h"
using namespace std;
class Organism
{
public:
virtual void move() = 0;
};
#endif
Organism.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Ant.h"
#include "Doodlebug.h"
#include "Organism.h"
#include "WorldMap.h"
using namespace std;
void Organism::move()
{
cout << "Organism move";
}
Prey.h
#ifndef _PREY_H
#define _PREY_H
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Organism.h"
using namespace std;
class Prey: public Organism
{
public:
Prey();
void move();
};
#endif
Prey.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include "Prey.h"
#include "Organism.h"
#include "WorldMap.h"
using namespace std;
void Prey::move()
{
cout << "Prey move";
}
This is the function I use to create the array. I am willing to bet my trouble lies here.
void WorldMap::createDynamicArray()
{
grid = new Organism* [gridHeight];
for(int i=0;i<gridHeight;i++)
*(grid+i)=new Organism[gridWidth];
}
And grid is instantiated using
Organism **grid;
The error I keep getting is
1> error C2259: 'Organism' : cannot instantiate abstract class
1> due to following members:
1> 'void Organism::move(void)' : is abstract
I have been stumped on this for hours, and really think I can go on to finish this thing as soon as I get this issue out of the way.
Thanks for your help!

New Topic/Question
Reply




MultiQuote




|