Function Overloading

I'm not able to call the proper overload for a certain function.

Here are the prototypes for the overloaded function.
1
2
3
4
5
6
7
8
9
10
class cItemMgr{
public:
	//**irrelevant members**//

	static bool CreateItem(char*, ITEMTYPE, const double, const int, const int, const int, const int, const char, const char, const char, const char, const char, const char, const int, const int, const int, const int, const int); //weapon
	static bool CreateItem(char*, ITEMTYPE, const double, bool (*)(void)); //ring 
	static bool CreateItem(char*, ITEMTYPE, const double, const char, const double, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int); //armor

	//**irrelevant members**//
};


Here are the calls to the overloaded function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool LoadAllItems(){
        //First overload
	cItemMgr::CreateItem("Longsword", RIGHTLEFT, 3.0, 10, 9, 0, 0, 'C', 'C', 'n', 'n', 'n', 'n', 112, 0, 0, 0, 0);
	cItemMgr::CreateItem("Shortsword", RIGHTLEFT, 2.0, 7, 10, 0, 0, 'C', 'C', 'n', 'n', 'n', 'n', 100, 0, 0, 0, 0);
        //Second overload
	cItemMgr::CreateItem("Life Ring", RING, 0.2, *cring::EffLifeRing);
	cItemMgr::CreateItem("Cloranthy Ring", RING, 0.2, *cring::EffClorRing);
        //Third overload
	cItemMgr::CreateItem("Alonne Knight Helm", ARMORHEAD, 4.6, 'C', 9, 40, 47, 52, 49, 9, 15, 7, 9, 5, 10, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Chest", ARMORCHEST, 14.2, 'A', 34, 152, 146, 161, 152, 27, 46, 22, 27, 14, 32, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Gauntlets", ARMORHAND, 6.7, 'C', 12, 72, 69, 76, 72, 13, 21, 10, 13, 7, 15, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Leggings", ARMORLEG, 9.8, 'C', 20, 105, 100, 111, 105, 19, 31, 15, 19, 10, 22, 0, 0);
	cItemMgr::CreateItem("Alva Helm", ARMORHEAD, 3.7, 'C', 6, 38, 36, 39, 37, 10, 11, 8, 11, 11, 15, 0, 0);
	cItemMgr::CreateItem("Alva Armor", ARMORCHEST, 8.8, 'A', 15, 89, 85, 92, 88, 25, 27, 19, 26, 26, 35, 0, 0);
	cItemMgr::CreateItem("Alva Gauntlets", ARMORHAND, 2.9, 'C', 4, 29, 28, 30, 29, 8, 9, 6, 9, 9, 11, 0, 0);
	cItemMgr::CreateItem("Alva Leggings", ARMORLEG, 5.2, 'C', 9, 53, 50, 55, 52, 15, 16, 11, 16, 16, 20, 0, 0);
	return true;
}



The first two calls to CreateItem (longsword and shortsword) execute properly, but the rest of the calls to CreateItem don't execute. At all. Confirmed by benchmark calls to fputs to a .txt file at the beginning of each CreateItem overload.
If more information is needed, I'll certainly post more. I'm simply weary of unnecessary walls of code.
What do all those parameters mean? There may be a better way to go about this than having such long parameter lists.
You could do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Item
{
     public:
        Item();
        virtual ~Item();
        ...
};

class Armor : public Item
{
    ...
};

class Weapon : public Item
{
    ...
};

...

std::vector<Item *> inventory;
Do you get an error when compiling the code? Shouldn't *cring::EffLifeRing be &cring::EffLifeRing?
The parameters are the various stats that correspond to the data members of each item type.

I need to create a registry of all the items in the game. I've divided the items into classes that correspond to the slot that the item occupies when it's equipped. Since I know exactly how many items exist, and they're all being hardcoded into the program, there's no need to use a vector or the new operator. So I explicitly allocate the space for the items into arrays.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define NUMWEAPON 2
#define NUMRING 2
#define NUMARMORHEAD 2
#define NUMARMORCHEST 2
#define NUMARMORHAND 2
#define NUMARMORLEG 2

class cItemMgr{
public:
	static cweapon rightlefth[NUMWEAPON];
	static cring ring[NUMRING];
	static carmorhead armorhead[NUMARMORHEAD];
	static carmorchest armorchest[NUMARMORCHEST];
	static carmorhand armorhand[NUMARMORHAND];
	static carmorleg armorleg[NUMARMORLEG];

	static bool CreateItem(char*, ITEMTYPE, const double, const int, const int, const int, const int, const char, const char, const char, const char, const char, const char, const int, const int, const int, const int, const int); //weapon
	static bool CreateItem(char*, ITEMTYPE, const double, bool (*)(void)); //ring
	static bool CreateItem(char*, ITEMTYPE, const double, const char, const double, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int, const int); //armor
};


Obviously there's going to be more than two of each type, but it doesn't make sense to hardcode information for 300some items when it's perfectly possible that the way I go about this will change.

The parameter list for the constructor for each of the 6 classes are identical to the parameter list for the CreateItem function. I'm fairly certain the CreateItem function is necessary because creating items directly through the constructor would cause problems with scope, so the purpose of the CreateItem function is to take the parameters given, decide which overload to use based on the parameters, create a temporary instance of the item with those parameters, assign the next element of the appropriate array to the newly created item instance, and then return. That way there won't be two copies of each item floating around in the code.

The first overload of CreateItem, the one that works and calls properly, looks like this.
1
2
3
4
5
6
7
8
9
bool cItemMgr::CreateItem(char* mname, ITEMTYPE mtype, const double weight, const int nstrreq, const int ndexreq, const int nintreq, const int nfthreq, const char strscl, const char dexscl, const char mgcscl, const char firescl, const char lghtscl, const char drkscl, const int physdmg, const int mgcdmg, const int firedmg, const int lghtdmg, const int drkdmg){
	static int iii = 0;
	if (mtype != RIGHTLEFT) //check to make sure the proper overload was called
		error(ERROR_1);
	if (iii >= NUMWEAPON)
		error(ERROR_2);
	cItemMgr::rightlefth[iii] = cweapon(mname, mtype, weight, nstrreq, ndexreq, nintreq, nfthreq, strscl, dexscl, mgcscl, firescl, lghtscl, drkscl, physdmg, mgcdmg, firedmg, lghtdmg, drkdmg);
	iii++;
	return true;


That gets called properly for the first two calls
1
2
cItemMgr::CreateItem("Longsword", RIGHTLEFT, 3.0, 10, 9, 0, 0, 'C', 'C', 'n', 'n', 'n', 'n', 112, 0, 0, 0, 0);
cItemMgr::CreateItem("Shortsword", RIGHTLEFT, 2.0, 7, 10, 0, 0, 'C', 'C', 'n', 'n', 'n', 'n', 100, 0, 0, 0, 0);


The next two CreateItem overloads, being
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
bool cItemMgr::CreateItem(char* mname, ITEMTYPE mtype, double nweight, bool (*RingEff)(void)){
	static int iii = 0;
	if (mtype != RING)
		error(ERROR_1);
	if (iii >= NUMRING)
		error(ERROR_3);
	cItemMgr::ring[iii] = cring(mname, RING, nweight, RingEff);
	iii++;
	return true;
}
bool cItemMgr::CreateItem(char* mname, ITEMTYPE mtype, const double nweight, const char strscale, const double poise, const int physdef, const int defvstrike, const int defvslash, const int defvthrust, const int defvmgc, const int defvfire, const int defvlght, const int defvdrk, const int psnres, const int bldres, const int ptrfres, const int cursres){
	if (mtype != ARMORHEAD && mtype != ARMORCHEST && mtype != ARMORHAND && mtype != ARMORLEG)
		error(ERROR_1);
	static int headcount, chestcount, handcount, legcount = 0;
	switch (mtype){
	case ARMORHEAD:
		if (headcount >= NUMARMORHEAD)
			error(ERROR_4);
		cItemMgr::armorhead[headcount] = carmorhead(mname, mtype, nweight, strscale, poise, physdef, defvstrike, defvslash, defvthrust, defvmgc, defvfire, defvlght, defvdrk, psnres, bldres, ptrfres, cursres);
		headcount++;
		break;
	case ARMORCHEST:
		if (chestcount >= NUMARMORCHEST)
			error(ERROR_5);
		cItemMgr::armorchest[chestcount] = carmorchest(mname, mtype, nweight, strscale, poise, physdef, defvstrike, defvslash, defvthrust, defvmgc, defvfire, defvlght, defvdrk, psnres, bldres, ptrfres, cursres);
		chestcount++;
		break;
	case ARMORHAND:
		if (handcount >= NUMARMORHAND)
			error(ERROR_6);
		cItemMgr::armorhand[handcount] = carmorhand(mname, mtype, nweight, strscale, poise, physdef, defvstrike, defvslash, defvthrust, defvmgc, defvfire, defvlght, defvdrk, psnres, bldres, ptrfres, cursres);
		handcount++;
		break;
	case ARMORLEG:
		if (legcount >= NUMARMORLEG)
			error(ERROR_7);
		cItemMgr::armorleg[legcount] = carmorleg(mname, mtype, nweight, strscale, poise, physdef, defvstrike, defvslash, defvthrust, defvmgc, defvfire, defvlght, defvdrk, psnres, bldres, ptrfres, cursres);
		legcount++;
		break;
	default:
		error(ERROR_8);
	}
	return true;
}

don't get called at all for the rest of the calls, which are
1
2
3
4
5
6
7
8
9
10
        cItemMgr::CreateItem("Life Ring", RING, 0.2, *cring::EffLifeRing);
	cItemMgr::CreateItem("Cloranthy Ring", RING, 0.2, *cring::EffClorRing);
	cItemMgr::CreateItem("Alonne Knight Helm", ARMORHEAD, 4.6, 'C', 9, 40, 47, 52, 49, 9, 15, 7, 9, 5, 10, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Chest", ARMORCHEST, 14.2, 'A', 34, 152, 146, 161, 152, 27, 46, 22, 27, 14, 32, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Gauntlets", ARMORHAND, 6.7, 'C', 12, 72, 69, 76, 72, 13, 21, 10, 13, 7, 15, 0, 0);
	cItemMgr::CreateItem("Alonne Knight Leggings", ARMORLEG, 9.8, 'C', 20, 105, 100, 111, 105, 19, 31, 15, 19, 10, 22, 0, 0);
	cItemMgr::CreateItem("Alva Helm", ARMORHEAD, 3.7, 'C', 6, 38, 36, 39, 37, 10, 11, 8, 11, 11, 15, 0, 0);
	cItemMgr::CreateItem("Alva Armor", ARMORCHEST, 8.8, 'A', 15, 89, 85, 92, 88, 25, 27, 19, 26, 26, 35, 0, 0);
	cItemMgr::CreateItem("Alva Gauntlets", ARMORHAND, 2.9, 'C', 4, 29, 28, 30, 29, 8, 9, 6, 9, 9, 11, 0, 0);
	cItemMgr::CreateItem("Alva Leggings", ARMORLEG, 5.2, 'C', 9, 53, 50, 55, 52, 15, 16, 11, 16, 16, 20, 0, 0);
Peter - No syntactical errors are present in the code.
giblit - yes, that's what I've done. Except for a single inventory doesn't make sense because the program isn't a game, it's a character builder for a game. It allows you to plan out your character. Having a single "inventory" would waste a lot of processing time, because when you click on your empty equipment slot, I'd have to call a function to sort the entire inventory, pick out all of the ones that correspond to the clicked equipment slot, create an entirely new array/vector, and return that new array/vector to be displayed in the drop-down menu.

Right...?
Topic archived. No new replies allowed.