no appropriate default constructor available

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(const char* 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();
}
Last edited on
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 ...)
{

}
¿Are you sure that you want inheritance there?
Last edited on
+1 to ne555

Inheritance forms an "is a" relationship. For example "Car" might inherit from "Vehicle" because a car is a vehicle.

Having ManageTowers inherit from Tower suggests that a ManageTowers is a Tower, which doesn't really make sense.
@ Peter87
thank I'll try that now

@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.

Maybe...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//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.)" 
Last edited on
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.


Last edited on
thank guys
now i understand. I did this:-

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include "Tower.h"
#include "Player.h"

class ManageTowers
{
public:
	ManageTowers();
	~ManageTowers();


	std::vector<Tower*> Towers;
};


its now working correctly. :D
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.
Topic archived. No new replies allowed.