I am trying to create a weapon class for a game. This is part of the code that I came up with to fit my needs:
class weapon {
public:
int fireRate;
int bulletDamage;
int range;
ofImage sprite;
ofImage bulletSprite;
bullet bullets[50];
int activeBullet;
public:
void fire();
};
class machineGun: public weapon {
public:
void fire();
};
class flamer: public weapon {
public:
void fire();
};
And then I would like to define an array of weapons as follows:
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
I would like element [0] to represent the class machineGun and element [1] to represent the class flamer. Am I doing this problem right? Should I somehow reorganize this? How to reach the array in which these two different types of weapons are stored?
The idea is that when I call weapons[0].fire();, I get the machineGun class, and when I call weapons[1].fire();, I get fire fire.
: , . " [0] = new machineGun;". " 0" .
- , ? :
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons];
weapons[0] = new machineGun;
weapons[1] = new flamer;
:
1>gameplay.cpp(49) : error C2466: cannot allocate an array of constant size 0
1>gameplay.cpp(49) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(49) : error C2371: 'weapons' : redefinition; different basic types
1> gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(49) : error C2440: 'initializing' : cannot convert from 'machineGun *' to 'int []'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>gameplay.cpp(50) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>gameplay.cpp(50) : error C2369: 'weapons' : redefinition; different subscripts
1> gameplay.cpp(48) : see declaration of 'weapons'
1>gameplay.cpp(50) : error C2440: 'initializing' : cannot convert from 'flamer *' to 'int [1]'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
. , .
const int totalWeapons = 2;
int currentWeapon = 1;
weapon weapons[totalWeapons] = {new machineGun, new flamer};
:
1>gameplay.cpp(48) : error C2275: 'machineGun' : illegal use of this type as an expression
1> gameplay.h(36) : see declaration of 'machineGun'
1>gameplay.cpp(48) : error C2275: 'flamer' : illegal use of this type as an expression
1> gameplay.h(41) : see declaration of 'flamer'
:
const int totalWeapons = 2;
int currentWeapon = 1;
weapon *weapons[totalWeapons] = {new machineGun, new flamer};
, !