Multiple definitions of

I am making Player with an inventory of Item objects. in the process of adding and removing items to the Player i get an error that says multiple definitions of various functions from my Items List. Could someone please help me out with this?

Also, i am not sure if having the ItemList in a .h is the best idea. but that was how i was getting it to work before i had this error come up.

I did not think it would be beneficial to make an object out of the item list because I will only ever have 1 Item list so that is why i put it in the header file on its own.
What would be a better way of doing that? or is that ok?

If you'd like to view all of the files in the project, you can download them here(NOTE: i will probably take the temp site down when i get an answer):
https://sites.google.com/site/tempforquestioncplusplus/download

Thanks in advance for the help!

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
45
46
47
48
49
50
51
52
53
54
55
56

ItemList.h

#include "Item.h"

Item ItemList[]{
//populated with ItemObjects
}
void PrintList({
//prints the inventory
}

main.cpp

#include <iostream>

#include "ItemList.h"
#include "Player.h"

int main(int argc, char *argv[]) {
	
	Player player("Player1");
	
	player.setPrimary(ItemList[4]);
	player.setSecondary(ItemList[6]);
	
	std::cout << "\n\n" << player.toString();
	
	return 0;
}

Player.cpp

#include "ItemList.h"

void Player::addItem(Item AddItem){
	
	for(int i = 0; i < InventoryLength; i++){
		
		if(Inventory[i].getName() == ItemList[0].getName()){
			
			Inventory[i] = AddItem;
		}
	}
}
void Player::removeItem(Item RemoveItem){
	
	for(int i = 0; i < InventoryLength; i++){
		
		if(Inventory[i].getName() == RemoveItem.getName()){
			
			Inventory[i] = ItemList[0];
		}
	}
}



multiple definition of `ItemListByte()'
first defined here
multiple definition of `ItemListSize()'
first defined here
multiple definition of `PrintList()'
first defined here
multiple definition of `ItemList'
first defined here
bad reloc address 0x14 in section `.text$_ZN4ItemD1Ev
Last edited on
You define functions and variables in your ItemList.h.
You must not do that, .h are only for declaring, defining happens in .cpp files.

Anyway, to the problem: removing #include "ItemList.h" (EDIT: in your main.cpp) should do the trick since you won't have two definitions of the functions in ItemList.h
Last edited on
that is what i thought also. but when i remove ItemList.h from my main.cpp then the error changes to ItemList[] not declared in this scope. I understand that if i call #include "ItemList.h " in 2 different places then it will be defining the header 2 times. but i am not seeing why i cannot use it across my Player and main files.
Ok i just did a work around. instead of using the #include "ItemList.h" in my main.cpp i redefined my function in the player to just call an int rather then an item that way it is the only one that deals with the specific header.
i redefined my function in the player to just call an int rather then an item that way it is the only one that deals with the specific header.

You're missing the point Mops made.

Line 6-8: Include only declarations, not definitions in a header file. Because you're naming and initializing the array, you're creating a definition. When you include the header file in multiple .cpp files, you end up with multiple definitions of ItemList, which the linker will flag as an error. If you want to define ItemList and populate it, do so at line 19. It's okay to declare ItemList as extern in a header file to indicate it is defined in another module.

Line 9-11: Include only function declarations, not definitions in a header file. Same reasons as above.

Last edited on
Oh, I see. I didn't get that. I have fixed it now. Thank you very much for the help!
Topic archived. No new replies allowed.