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:
#ifndef SWARM_OBJECT_H
#define SWARM_OBJECT_H
#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
{
public:
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;
protected:
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;
private:
bool detectCollision(Object *);
static int ObjectCount;
};
#endif
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
{
public:
//Init
Ship(float x = 0, float y = 0);
Ship(char* model_filename, float x = 0, float y = 0);
~Ship();
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;
protected:
Vector2D facing;
void tilt(float seconds);
void friction(float seconds);
virtual void generateRadius();
private:
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
};
#endif
here''s my projectile, which also compiles:
#ifndef WEAPONS_H
#define WEAPONS_H
#include "Object.h"
class Projectile : public Object
{
public:
Projectile(float x, float y, float radius, Vector2D direction, float fRed = 1, float fGreen = 0.5, float fBlue = 0);
void CollideObject(Object *);
protected:
float colors[2];
float speed;
};
#endif
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));
}