Sign in to follow this  
Psych0 P3ngu1n

error C2512: 'Weapon' : no appropriate default constructor available, Please help me.

Recommended Posts

#include<iostream>
#include<string>
using namespace std;

class Weapon
{
protected:
	int Damage;
public:
	Weapon(int);
	virtual void Hit();
};
Weapon :: Weapon(int dm)
{
	Damage = dm;
}
class Sword : public Weapon
{
protected:
	int Slash;
public:
	Sword(int);
	virtual void Strike();
};
Sword :: Sword(int sl)
{
	Slash = sl;
}
class Paladin
{
protected:
	int health;
	int attack;
	int strength;
public:
	Paladin();
	Weapon *weapon;
	void Ryan(int);
	void WeaponWield();
	void Hitting();
}*PaladinPtr;

Paladin :: Paladin()
{
	attack = 100;
	strength = 100;
	Weapon *weapon = new Sword(300);
}

int main()
{
	PaladinPtr = new Paladin();
	PaladinPtr -> WeaponWield();
	Paladin Culotte;
	Culotte.Hitting();
	Culotte.Ryan(75);
	return 0;
}

void Paladin :: Ryan(int heal)
{
	strength = 50;
	health = heal;
	cout<<"Culotte leveled up to Ryan."<<endl;
	cout<<"Ryan's new health is "<<health<<"."<<endl<<endl;
}
void Paladin :: WeaponWield()
{
	cout<<"Culotte became a Paladin."<<endl;
	cout<<"You are wielding a weapon."<<endl;	
}
void Paladin :: Hitting()
{
	cout<<"You are now attacking the target."<<endl;
	cout<<"The target is attacking back."<<endl;
	cout<<"You killed the target."<<endl<<endl;
}


Error comes up on the Sword :: Sword constructor, why? [edit: source tags are your friend -SiCrane]

Share this post


Link to post
Share on other sites
If you're not explicitly constructing the base class from the derived class, the base class' default constructor is used to construct the base class. Since the Sword class doesn't construct its Weapon base, the Weapon base will be default constructed, but it doesn't have a default constructor.

Sword :: Sword(int sl) : Weapon(...)
{
Slash = sl;
}

Look up initialization lists. Replace ... with whatever you want to construct the Weapon base with.

Share this post


Link to post
Share on other sites
Ignoring the other problems with your code...

Weapon has no public, default constructor (it has one constructor that takes an int). Sword is a subclass of Weapon. Sword has a single constructor (it also takes an int), but that constructor does not specify which base class constructor to call, which means you're asking the compiler to call the default. But there is none, thus the error.

To fix this, you need to specifiy which base class constructor to call in the constructor for Sword:

Sword::Sword(int sl)
: Weapon(sl) {
Slash = sl;
}

Share this post


Link to post
Share on other sites
ok after putting in your suggestions i get these errors. I know my code is full of errors, im actually brand new so im comin here for help ><


error LNK2001: unresolved external symbol "public: virtual void __thiscall Weapon::Hit(void)" (?Hit@Weapon@@UAEXXZ)

error LNK2001: unresolved external symbol "public: virtual void __thiscall Sword::Strike(void)" (?Strike@Sword@@UAEXXZ)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this