Sign in to follow this  
665.999

Problem creating an item in Text Game

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 :D .

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 this post


Link to post
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.[/size][/color]

Share this post


Link to post
Share on other sites
Plus one for using the constructor to initialize your member variables. In fact, you should get familiar with [url="http://www.cprogramming.com/tutorial/initialization-lists-c++.html"]Initialization Lists[/url]

Your "[color="#1C2837"][size="2"]Multiple definition of..." error is most likely due to not having [url="http://en.wikipedia.org/wiki/Include_guard"]Include guards[/url]. That is, however, just a guess, since there is no code to look at.[/size][/color]

Share this post


Link to post
Share on other sites
Are you familiar with how to [url="http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798"]organize code files in C and C++[/url]?

Share this post


Link to post
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++.
Thanks for your time :) .

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****

[i]#include <iostream>[/i]
[i]using namespace std;
[/i]#include class.h

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

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

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

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

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

***CLASS.H****

[i]iostream, namespace etc...
[/i]
#ifndef CLASS_H
#define CLASS_H

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

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

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

#endif // CLASS_H

Share this post


Link to post
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 this post


Link to post
Share on other sites
[quote="665.999"][color="#1C2837"][size="2"]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][/size][/color]
[color="#1C2837"][size="2"]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.[/size][/color]

[color="#1C2837"][size="2"]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.[/size][/color]

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

[size="2"][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.[/color][/size]

Share this post


Link to post
Share on other sites
[quote name='665.999' timestamp='1317687907' post='4868765']
My compiler is Dev-C++.
[/quote]
Egad, man. Go get [url="http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express"]MSVC 2010 Express[/url] or [url="http://www.codeblocks.org/"]Code::Blocks[/url] 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.

Looking at your code, it seems you're not clear about the difference between declaration and definition. You want your declarations in header (H/HPP) files, and our definitions in source (CPP) files. Then include the header files where you need them. Don't include source files. What's happening is that [b]class.cpp[/b] is getting compiled into [b]class.o[/b]; [b]func1.cpp[/b] includes [b]class.cpp[/b] and is getting compiled into [b]func1.o[/b]; and [b]maincpp[/b] includes [b]func1.cpp[/b], which includes [b]class.cpp[/b], and is getting compiled into [b]main.o[/b]. Then when the linker goes to link them together it finds two copies of [b]func1()[/b] and three copies of [b]CHRCTR[/b]'s methods, and aborts. Using function and class declarations in header files lets you tell the various source files, "This is defined somewhere else, but don't worry about it--you'll all meet up in the linker." The article to which I linked above explains how to do this properly. Read it and try to follow it. If you're still stuck afterward we can help sort you out.

Share this post


Link to post
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; [b]// the amount of HGBA[/b]
public:
int getHGBA(); [b]// Should be able to get how much I have?[/b]
void setHGBA(int); [b]// Should be able to set how much I have?[/b]
};

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 this post


Link to post
Share on other sites
[quote name='665.999' timestamp='1318205468' post='4870915']
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; [b]// the amount of HGBA[/b]
public:
int getHGBA(); [b]// Should be able to get how much I have?[/b]
void setHGBA(int); [b]// Should be able to set how much I have?[/b]
};

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!
[/quote]

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


[i]using namespace std[/i] 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

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

int CHRCTR::getHGBA()
{
return HGBA;
}[/i]

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

Share this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='Wooh' timestamp='1318213257' post='4870930']
How and where is your definition of Player?
[/quote]
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 this post


Link to post
Share on other sites
[quote name='665.999' timestamp='1318211407' post='4870926']
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?
[/quote]

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
[code]

#include "Class.h"
#include "Func1.h"

int main()
{
CHRCTR Player;

Func1(Player);

Player.setHGBA(20);

...
}
[/code]


Func1.h
[code]

...
#include "Class.h"

void Func1(CHRCTR& player);
...
[/code]

Func1.cpp
[code]

#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
}
[/code]

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 this post


Link to post
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".

Outside of the code, how would you guys go about doing this? On a high level, I mean. If you had an integer you need to be able to access inside every function in your entire program (ie. in an RPG, you need to be able to see your HP in battle and in your menu and adjust your HP, during battle and using items in your menu etc.), how would you go about implementing that in your program? Maybe I'm trying to do it in a wrong way and there's a better solution?

Share this post


Link to post
Share on other sites
[quote name='665.999' timestamp='1318215692' post='4870941']
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".

Outside of the code, how would you guys go about doing this? On a high level, I mean. If you had an integer you need to be able to access inside every function in your entire program (ie. in an RPG, you need to be able to see your HP in battle and in your menu and adjust your HP, during battle and using items in your menu etc.), how would you go about implementing that in your program? Maybe I'm trying to do it in a wrong way and there's a better solution?
[/quote]

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.

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