# Problem creating an item in Text Game

This topic is 2324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Everybody!

I'm new to C++ and I'm trying my most ambitious project yet; an RPG styled text version of Resident Evil 3.

I'm having problems with creating an item though. Let's say we have item HandGunAmmo. I create a struct in the header file "Items.h" called GunAndAmmo.
I have an int variable declared called InvAmount. I then create an instance called HandGunAmmo.
I create a function called void DeclareAllItems(), that I use to make sure all the items values start at 0.
Therefore, HandGunAmmo.InvAmount = 0. We have 0 Handgun Bullets.
So far this has worked in my program. However, when I try and create the battle loop and include the "Items.h" file, it says;

"Multiple definition of DeclareAllItems()"

"Multiple definition of HandGunAmmo"

What I want to do is create an int value starting at 0. As you go through the game, you can find ammo (therefore increasing the value).
When you battle and use bullets, it decreases the value. However it won't let me access the variable again to modify it.
At any point in the game I need to be able to access this variable so the player knows how much bullets he has,
And when he starts a battle, he begins with the amount of bullets he has had up until that battle.

Should I be using a pointer to point at HandGunAmmo.InvAmount? Should I not be declaring it in a struct, instead declaring it as a global variable?

Any help is very useful and appreciated. If any more info is needed, I am happy to share it (even uploading all the source code).
Thanks for your time guys .

Edit: The reason I am not just posting my source code right away is because I want to try and understand the possible concepts I am getting wrong, not the code.
I am doing pretty good at coding, but sometimes the concepts are hard to grasp (especially Classes atm). So if I shouldn't be using a structure or should be using a pointer or whatever the problem is, I'd rather find out conceptually what I'm doing wrong then just being handed the code .

##### Share on other sites
Instead of having a function [color=#1C2837][size=2]DeclareAllItems(), you should just use the constructor to initiate variables of a struct or a class. I won't be able to tell you why you are getting errors without seeing any code.

##### Share on other sites
Plus one for using the constructor to initialize your member variables. In fact, you should get familiar with Initialization Lists

Your "[color="#1C2837"]Multiple definition of..." error is most likely due to not having Include guards. That is, however, just a guess, since there is no code to look at.

##### Share on other sites
Are you familiar with how to organize code files in C and C++?

##### Share on other sites
Hey guys, thanks for the replies. I'm reading up on the links you have suggested to me.

Here is a basic version of the code (without all the plot and whatnot of the text game I'm making) so you can see the basic issue. It comes up with the same compiling problems.

HGBA = HandGunBulletAmount. My compiler is Dev-C++.

Edit: This text has been motified from what I've learned from comments below, but it is still not working. I have changed my compiler to Visual Studios 2010. The error I am getting is "already defined".

****MAIN.CPP****

#include <iostream>
using namespace std;
#include class.h

void Func1(); // Declaring the prototype from the other file. This is where the story would start

int main() {
Func1(); // Send it to the FUNC1.CPP where I can start the game
Player.setHGBA(20); // I should be able to change the value after Func1, or anywhere else I want to in the game and source code
system("pause");
return 0;
}

****FUNC1.CPP*****

iostream, namespace std etc;
#include "Class.h" // Include the class that holds HGBA

void Func1()
{
CHRCTR Player;
Player.setHGBA(10); // start the player's amount at 10
cout << Player.getHGBA() << endl; // test to see if I can see that the HGBA amount is 10
}

***CLASS.H****

iostream, namespace etc...

#ifndef CLASS_H
#define CLASS_H

class CHRCTR
{
private:
int HGBA; // the amount of HGBA
public:
int getHGBA(); // Should be able to get how much I have?
void setHGBA(int); // Should be able to set how much I have?
};

void CHRCTR::setHGBA(int bullamount)
{
HGBA = bullamount;
}

int CHRCTR::getHGBA()
{
return HGBA;
}

#endif // CLASS_H

##### Share on other sites
You should never include .cpp files. Instead create a header file that contains the class definition and/or function declarations. You will need to use include guards in the header files. Read the link that yckx posted. It is very good and explains how to handle this.

##### Share on other sites
[quote="665.999"][color="#1C2837"]Should I be using a pointer to point at HandGunAmmo.InvAmount? Should I not be declaring it in a struct, instead declaring it as a global variable?[/quote]
[color="#1C2837"]A pointer should not be needed, but that's a much deeper subject than we should go into, for now. It's generally thought of as bad practice to have global variables. I've got a question. Does the HandGunAmmo struct/class have anything other than the InvAmount? If it's just storing an int value, I wouldn't bother dedicating a struct to it. HandGunAmmo (are you going to have one type of handgun ammo?) should be part of the player (or CHRCTR) class...maybe part of an inventory class, that is a member variable of the play class.

[color="#1C2837"]The biggest problem is, there isn't one way of doing things and there are a lot of design decisions that can invalidate any, or all, of what was just said.

[color="#1C2837"]As Wooh said, you should create header files (with include guards!), for your cpp files, and include those, instead of your cpp files.

[color="#1c2837"]It's hard to tell if this is an issue, because there was no compilable code given, but, based on what you've supplied, it looks like you never include the Func1() definition into the main.cpp, where it's called.

##### Share on other sites

My compiler is Dev-C++.

Egad, man. Go get MSVC 2010 Express or Code::Blocks or some other current, maintained IDE immediately. Dev-C++ is so old it doesn't list Vista among its supported systems. Because it hadn't come out yet at the time of Dev-C++'s latest release.

##### Share on other sites
Thanks for the replies! I've switched to Visual Studio 2010 as my C++ Compiler, thanks for the suggestion .

So from what I gather from the links to info on inclusion guards, I should change the Class.cpp file to Class.h with inclusion guards, like so:

/* **CLASS.H*** */

#include <iostream>
using namespace std;

#ifndef CLASS_H
#define CLASS_H

class CHRCTR
{
private:
int HGBA; // the amount of HGBA
public:
int getHGBA(); // Should be able to get how much I have?
void setHGBA(int); // Should be able to set how much I have?
};

void CHRCTR::setHGBA(int bullamount)
{
HGBA = bullamount;
}

int CHRCTR::getHGBA()
{
return HGBA;
}

#endif // CLASS_H

Edit : I've edited my post above with my source code to match what I currently have on my 'updated (but still not working)' file.

However, it still doesn't work, and I get the same "already defined" errors. If there are any more tips to offer, please let me know . This is the main thing holding me back from finishing my text based game!

##### Share on other sites

Thanks for the replies! I've switched to Visual Studio 2010 as my C++ Compiler, thanks for the suggestion .

So from what I gather from the links to info on inclusion guards, I should change the Class.cpp file to Class.h with inclusion guards, like so:

/* **CLASS.H*** */

#include <iostream>
using namespace std;

#ifndef CLASS_H
#define CLASS_H

class CHRCTR
{
private:
int HGBA; // the amount of HGBA
public:
int getHGBA(); // Should be able to get how much I have?
void setHGBA(int); // Should be able to set how much I have?
};

void CHRCTR::setHGBA(int bullamount)
{
HGBA = bullamount;
}

int CHRCTR::getHGBA()
{
return HGBA;
}

#endif // CLASS_H

Edit : I've edited my post above with my source code to match what I currently have on my 'updated (but still not working)' file.

However, it still doesn't work, and I get the same "already defined" errors. If there are any more tips to offer, please let me know . This is the main thing holding me back from finishing my text based game!

The inclusion guards should be at the very top of the file.

using namespace std should also go in the CPP file. Pulling in an entire namespace like that in a header file can give you problems later on.

The CHRCTR class goes in the header file but this part

void CHRCTR::setHGBA(int bullamount)
{
HGBA = bullamount;
}

int CHRCTR::getHGBA()
{
return HGBA;
}

goes in the CLASS.CPP file. The header files should contain only declarations, not definitions.

##### Share on other sites
Thanks pulpfist!

Now the only problem is that it says "class CHRCTR Player already defined", since I'm calling the Player.setHGBA() method both in the Func1 and Main functions (or at least I think that's why). I want to be able to change the HGBA variable at any point of the program, so is there a different way I should be doing this or is there some code I'm missing/doing wrong?

##### Share on other sites
How and where is your definition of Player?

##### Share on other sites

How and where is your definition of Player?

I create Player in Func1(), CHRCTR Player;

I then also call methods of Player in the Func1 Function, and in Main after Func1 is over.

##### Share on other sites
If Player is defined locally in Func1 how can you access it from main?

##### Share on other sites

Thanks pulpfist!

Now the only problem is that it says "class CHRCTR Player already defined", since I'm calling the Player.setHGBA() method both in the Func1 and Main functions (or at least I think that's why). I want to be able to change the HGBA variable at any point of the program, so is there a different way I should be doing this or is there some code I'm missing/doing wrong?

If you need access to Player in both main and Func1, I would suggest you create the player it in main and send it as a reference parameter to Func1

main.cpp
 #include "Class.h" #include "Func1.h" int main() { CHRCTR Player; Func1(Player); Player.setHGBA(20); ... } 

Func1.h
 ... #include "Class.h" void Func1(CHRCTR& player); ... 

Func1.cpp
 #include "Func1.h" void Func1(CHRCTR& player) { player.setHGBA(10); // start the player's amount at 10 cout << player.getHGBA() << endl; // test to see if I can see that the HGBA amount is 10 } 

Something like that.
As you can see I have placed the Func1 prototype in a file Func1.h, and included that in main.
And the Func1 now takes a reference to a CHRCTR as parameter.
I have also left out a lot of code like the inclusion guards. I guess you can fix that yourself.

I'm not sure exactly how you want things but maybe that example can help a bit

##### Share on other sites
Thanks a lot for the suggestions guys! I tried declaring it in main instead of Func1, and I tried passing Player as an argument. It still comes up with the same error "already defined".

##### Share on other sites

Thanks a lot for the suggestions guys! I tried declaring it in main instead of Func1, and I tried passing Player as an argument. It still comes up with the same error "already defined".

It depends on a lot of things, like what kind of game, how big is the project, is it a one man or a team effort, what are the skills of the coders, etc.
Basically, different circumstances requires different strategies, and as always there is many ways to go about it.

Thats pretty much all I can say with my limited experience with large projects.

PS.
If you paste the whole error message its easier for us to give you tips about what could be wrong.