For whatever reason, I can't figure this out.

I'm getting this error:

error C2082: redefinition of formal parameter 'user'

In this code:

#pragma once
#include "Standard Libs.h"
#include "Ability.h"
#include "Spell.h"
#include "Item.h"

class Adventurer
{
private:
	string name;

	int level;
	int xp;
	int gold;
	int attack;
	int defence;
	int max_hp;
	int hp;
	int max_mp;
	int mp;
	int max_tp;
	int tp;

	vector<Spell> spells;
	vector<Item> items;
	vector<Ability> abilities;

public:
	Adventurer();
	Adventurer(string user);

	virtual void set_name(string user) { name = name; }
	virtual void set_level(int level) { level = level; }
	virtual void add_xp(int xp) { xp += xp; }
	virtual void add_gold(int gold) { gold += gold; }
	virtual void add_hp(int hp) { hp += hp; }
	virtual void remove_hp(int hp) { hp -= hp; }
	virtual void add_mp(int mp) { mp += mp; }
	virtual void remove_mp(int mp) { mp -= mp; }
	virtual void add_tp(int tp) { tp += tp; }
	virtual void remove_tp(int tp) { tp -= tp; }

	/*
	virtual void add_spell(Spell);
	virtual void add_item(Item);
	virtual void add_ability(Ability);
	*/
};


#include "Adventurer.h"

Adventurer::Adventurer()
{
	name = "";
	cout << "Default." << endl ;
	cin.get();
}

Adventurer::Adventurer(string user)
{
	name = user;
	xp = 0;
	level = 1;
	gold = 50;
}



#include "Standard Libs.h"
#include "Adventurer.h"

class Warrior : Adventurer
{
public:
	Warrior(string user);
};


#include "Standard Libs.h"
#include "Warrior.h"
#include "Adventurer.h"

Warrior::Warrior(string user)
{
	Adventurer(user);
}

When I try to do:

Warrior war("Test");

Recommended Answers

All 2 Replies

I see 3 things:

  1. Warrior inherits privately from Adventurer
  2. You attempt to invoke an inherited constructor from a derived constructor's body.
  3. You are masking class members with local names.

RE #1:
Because of the private inheritance, all of the public members of Adventurer are private and any other members are inaccessible. I don't think this is your issue, but it's something to keep in mind.

RE #2:
You must invoke inherited constructors through an initialization list. You can't do it from the body of a derived constructor. This is because all constructors have finished by the time the body of the method is entered.

This:

Warrior::Warrior(string user)
{
	Adventurer(user);
}

Should be this:

Warrior::Warrior(string user) : Adventurer(user)
{
}

RE #3:
Take another look at your variable names in your Adventurer methods. They all have some form of masked identifier or misused identifier. For example, what does this do (here's a hint, not what you think):

virtual void set_name(string user) { name = name; }

There are multiple similar examples:

virtual void remove_hp(int hp) { hp -= hp; }

Q. What does this do? Which hp is which?
A. They're both local, neither one is Adventurer::hp. Both are Adventurer::remove_hp::hp. You should rename the parameter to something like "damage" to prevent variable masking/hiding.

virtual void remove_hp(int damage) { hp -= damage; }

Oops. I was aware of #1, and must have typo'd #3. #2 is exactly what I was looking for. Thanks!

edit> Didn't even realize #3 was a problem. Thanks for point that out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.