Hello guys
I have two classes one called Tower and the second called ManageTowers. ManageTowers inherit from Tower.
when ManageTowers inherit from Tower i get this error message "no appropriate default constructor available". why?!
1 2 3 4 5 6 7
class Tower
{
public:
Tower(constchar* towerImage, sf::Vector2<float> Position, int damage, float range, int firingDelayIn_ms, float bulletSpeed, int cost, int refundAmount, Player* InputPlayer);
~Tower();
}
1 2 3 4 5 6
class ManageTowers : Tower
{
public:
ManageTowers();
~ManageTowers();
}
Tower's constructor will run before ManageTowers's constructor. Because you didn't specified which constructor to use it uses the default constructor (constructor without arguments) but Tower doesn't have a default constructor so you get an error.
You can either create a default constructor for Tower or specify the constructor to use when defining ManageTowers constructor.
1 2 3 4 5
ManageTowers::ManageTowers()
: Tower( ... pass arguments here ...)
{
}
@Disch
well I want when every the mouse is clicked a Tower get spawned in my game, so I need a class to manage the towers, that's why i created the TowerManager.
Tower class will just create one tower and TowerManager will handle and store all my Towers in a vector.
just like a particle engine, first you create a single partical class that only make one particle and then the particle engine will inherit from particle class and will make more particles and manage them to make a shape.
//Do you want something like this, (not inheritance.)
//(This is a "has a" relationship, as in: "ManageTowers has a (or contains a) vector of Towers."
class ManageTowers {
private:
vector<Tower>;
...
};
//At the very least you probably want (as opposed to what you originally had:)
class ManageTowers : public vector<Tower> {
...
};
//As in: "ManageTowers is a vector (of Towers.)"
just like a particle engine, first you create a single partical class that only make one particle and then the particle engine will inherit from particle class and will make more particles and manage them to make a shape.
Only if you don't understand what classes are for.
In C++, when a derived class publicly inherits from a base class, it is a kind of base class. It can be used everywhere a base class can be used.
Is a particle engine a kind of particle? No. It's a box for putting particles in and doing things to them and keeping track of them. It is not a kind of particle. Having particle engine inherit from particle makes no sense.
Tower class will just create one tower and TowerManager will handle and store all my Towers in a vector.
This in no way suggests that TowerManager should be a kind of Tower. It does strongly suggest the first thing Mathhead200 said.
If you have polymorphism with the towers, that's fine.
However I suggest that you use smart pointers instead, like auto_ptr, so you don't have to worry about proper deleting.