• Advertisement


This topic is now archived and is closed to further replies.

Cannot instantiate abstract class? But its not abstract!!

This topic is 5061 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an abstract class Object. Ship inherits from it, with no problems. Projectile (weapons) inherits from it, but when I try to instantiate one, I get a compiler error: Cannot instantiate Abstract class. ???? here''s my abstract Object:


#include <list>
#include "includes.h"
#include "Vector2D.h"
#include "ResourceManager.h"
#include "Rectangle.h"
//#include "lwObjectLoader.h" //for 3D models

//class ResourceManager;

//also an abstract class. All game object classes inherit from this one.

class Object : public ResourceManager
	Object(float x = 0, float y = 0);
	virtual ~Object();
//---Pure virtual functions------------------------------

//(Stuff that HAS to be overridden in derrived classes)

	virtual void run(float elapsed_seconds, rect_t<float> *screen = NULL) = 0; //called every frame. //Do all the stuff you''re supposed to be constantly doing: movement, collision, etc..

	virtual void draw(bool ShowCircle = false) = 0;

	void move(float seconds); //let your momentum carry you

	virtual void CollideObject(Object *); //handles collision physics

	void WrapAround(rect_t<float> *screen);
	void SaveMomentum();

//----Utility Functions--------------------------------------------------------------

	Vector2D* getMomentumRef();
	Vector2D getMomentum();
	float getX();
	float getY();
	inline Vector2D getLocation();
	float getBoundingRadius();
	float getMass();
	void setLocation(float x, float y);
	static void addObject(Object *);

//----------Mass functions (functions that iterate through all objects in the world)--------------------------------------

	static void DrawObjects(bool ShowCircle = false); //draw ALL objects (TODO: pass in the frustum class)

	static Vector2D RunObjects(float seconds, rect_t<float> *screen = NULL, Object* player = NULL); //move ALL objects

	static void DoCollisions();

	Vector2D backupMomentum; //for collision

	Vector2D lastPosition;
	Vector2D location;

	Vector2D momentum;
	float BoundingRadius; //radius for the collision bounding circle

	virtual void generateRadius(); //generate a bounding circle for this object, based on its 3D model''s vertices or otherwise, if it has no 3D model

//----------------STATIC MEMBERS-----------------------------------------

	static std::list<Object *> objectList; //list of all game objects

	static std::list<Object *>::iterator itor;//iterator

	static std::list<Object *>::iterator inner;

	static float energyLoss; //how much energy % objects gain/lose when they collide. (0.01)

	static float maxMomentum; //the fastest an object can move, in units per second. 

	bool dead;		//If so, it will erase itself in the ''run'' cycle

	float mass;
	//	list<Object *> children;

	bool detectCollision(Object *); 
	static int ObjectCount;



here''s my ship, which works fine:

#ifndef SHIP_H
#define SHIP_H

#include "Object.h"
#include "lwvObject.h"
#include "ShipVars.h"
#include "Weapons.h"

enum Directions {RIGHT = 1, LEFT = 2, FORWARD = 3, BACK = 4};

class Ship : public Object



	Ship(float x = 0, float y = 0);
	Ship(char* model_filename, float x = 0, float y = 0);
	bool loadModel(char* filename);
	static void loadvars(char* filename); //call this only once and you will set the vars for all of the ship objects

	static void setVars(CShipVars *); //set up some new variables for the ships

//Per-Frame stuff

	virtual void run(float elapsed_seconds, rect_t<float> *screen = NULL); 
	virtual void draw(bool ShowCircle = false);
	void Thrust(float seconds);
	void Shoot(float seconds);
//Ship Controls

	void ChangeThrust(bool direction, float seconds); //true for forward, false for back.

	void setThrust(float amount); //set any thrust you like, but I won''t let you violate the min/max

	//TODO: ChangeThrust function that alters based on the mousewheel position

	void turn(int direction, float seconds); //the amount doesn''t allow turning faster than the ship''s max turning speed, but it does allow it to turn a little slower. Usefull for when the ship''s heading needs to match the turning reticle

	void PrintDebug();
	float GetThrust();
	void WrapAround(); //wraparound if you''re past the level bounds

	float GetRoll();
	int FontID;
	Vector2D facing;
	void tilt(float seconds);
	void friction(float seconds);
	virtual void generateRadius();
	float roll; //how much the ship is banked on its side

	float shotRadius; //how big the radii for the bullets are

	int turnState; //which way the ship is banking: RIGHT, LEFT or NULL (not banking)

	float thrustPower; 
	static CShipVars Vars; //holds many innocuous attributes shared among all ship objects.

//	bool useDefaults; //defaults to true: Decides wether this ship uses the same variables (rollspeed, turnspeed, maxSpeed, etc..) as all the other ships, or whether it has its own specialised paramaters

	lwvObject *pModel; //pointer to the 3D model the ship uses




here''s my projectile, which also compiles:

#ifndef WEAPONS_H
#define WEAPONS_H

#include "Object.h"

class Projectile : public Object
	Projectile(float x, float y, float radius, Vector2D direction, float fRed = 1, float fGreen = 0.5, float fBlue = 0);
	void CollideObject(Object *);
	float colors[2];
	float speed;


now here''s me trying to instantiate a projectile, but getting a compiler error.

void Ship::Shoot(float seconds)
//static float timeElapsed; //time elapsed since last shot

	//timeElapsed += seconds;

	//if(timeElapsed >= 

	//place shot just in front of ship.

	Vector2D shot = facing * BoundingRadius;
	addObject(new Projectile(shot.x, shot.y, shotRadius, facing));


Share this post

Link to post
Share on other sites
...did you read the comments in your own code?

"(Stuff that HAS to be overridden in derrived classes)"

Is that stuff overridden in your derived class Projectile? because it HAS to be, or Projectile will be an abstract class.

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on April 10, 2004 9:23:21 PM]

Share this post

Link to post
Share on other sites
Yes, you aren''t implementing the pure virtual methods defined in Object in Projectile.

Share this post

Link to post
Share on other sites

  • Advertisement