1 Replies - 4084 Views - Last Post: 03 December 2008 - 06:20 PM Rate Topic: -----

#1 torboxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-December 08

Dynamic 2D array of abstract/virtual class-objects

Post icon  Posted 03 December 2008 - 06:14 PM

I am working on an assignment which deals with polymorphism.

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic 2D array of abstract/virtual class-objects

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Dynamic 2D array of abstract/virtual class-objects

Posted 03 December 2008 - 06:20 PM

Couple things:

First the compiler is telling you the right things. you made Organism an abstract class. You made its only function a pure virtual one which means that derived classes HAVE to override it. So get rid of Organism::Move.

Second since Organism is abstract you cannot instantiate it (like the compiler says. Use Prey or make Organism a regular base class.

Your trouble lies within your framework. And because of that we may not be able to see other "real" problems. Hope that helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1