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 .
Problem creating an item in Text Game
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.
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.
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.
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****
#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
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****
#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
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.
[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.
[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.
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.
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 class.cpp is getting compiled into class.o; func1.cpp includes class.cpp and is getting compiled into func1.o; and maincpp includes func1.cpp, which includes class.cpp, and is getting compiled into main.o. Then when the linker goes to link them together it finds two copies of func1() and three copies of CHRCTR'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.
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!
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!
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement