Simple C++ Question...

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
ADCoffee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sat Apr 17, 2010 10:25 pm

Simple C++ Question...

Post by ADCoffee »

I have a c++ project that is expanding and I decided I would separate the classes from the main file, but I'm having a problem. In one of my classes I have a function that calls a member array of the class. I need to use this array in other files as well so how can I declare it so it can be used within the class's function and throughout the rest of the program.

(If I need to post and example I will in a little bit. I'm just really busy for a little while (Art project) figured I would try to get some help while I can't work on it.)

Edit:
Code is in my next post
Last edited by ADCoffee on Sat Apr 17, 2010 11:26 pm, edited 2 times in total.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Simple C++ Question...

Post by xiphirx »

With classes you should split them into headers and implementation files.

Example:

myclass.h

Code: Select all

#IFNDEF MYCLASS
#DEFINE
class myclass
{
myclass(int sx);
private:
int x;
}
#ENDIF

myclass.cpp (implementation)

Code: Select all

#include "myclass.h"

myclass::myclass(int sx)
{
x = sx;
}
(my code is very bad eh?)

now, whenever you need to use "myclass", you include myclass.h in the cpp file.

so if you had myclass, and myclass used another class named myotherclass, you would include myotherclass's header in myclass's header and implementation.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
ADCoffee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sat Apr 17, 2010 10:25 pm

Re: Simple C++ Question...

Post by ADCoffee »

Thanks for the reply.

I think I didn't explain myself fully. I'll just copy the code directly from my class's files. (The stuff I'm having trouble with is commented out.)

class header file:

Code: Select all

#ifndef armor
#define
 
#include <string>

using namespace std;

class armor {
public:
	string name;
	int type;
	int def;
	int dex;
	int str;
	int luk;
	int aid;
	//void useitem(int);
	static size_t count;
	static size_t howMany()
    { return count; }
	
	
	void puton(int);
	armor(string, int, int, int, int, int);
	armor();
	
};

#endif

class file:

Code: Select all

#include "Armor.h"
#include "Player.h"
#include <string>

using namespace std;

//armor helmets[300];

armor::armor(std::string n, int t, int df, int dx, int sr, int lk) {
	count++;
	name = n;
	type = t;
	def = df;
	dex = dx;
	str = sr;
	luk = lk;
	aid = 100 + (count % 100);
	
	/*helmets[aid].name = n;
	helmets[aid].def = df;
	helmets[aid].dex = dx;
	helmets[aid].str = sr;
	helmets[aid].luk = lk;*/
}

armor::armor() {
	count++;
	name = "Temp";
	type = 3;
	def = 1;
	dex = 0;
	aid = 100 + count;
}

/*void armor::useitem(int uid) {
	//int xx = 0;
	//for (int x = 0; x < armor::howMany() ; x++) {
	
	if (inv_default.search(uid) == true) {
		
		int typelo = uid % 100;
		
		if (playerOne.currentHelm > 100) {
			inv_default.Add(playerOne.currentHelm);
			if (helmets[playerOne.currentHelm].def > 0)
				playerOne.def -= helmets[playerOne.currentHelm].def;
			if (helmets[playerOne.currentHelm].dex > 0)
				playerOne.dex -= helmets[playerOne.currentHelm].dex;
			if (helmets[playerOne.currentHelm].str > 0)
				playerOne.str -= helmets[playerOne.currentHelm].str;
			if (helmets[playerOne.currentHelm].luk > 0)
				playerOne.luk -= helmets[playerOne.currentHelm].luk;
			
		}
		
		
		switch ((uid-typelo)/100) {
			case 1:
				playerOne.currentHelm = uid;
				playerOne.def += helmets[uid].def;
				playerOne.dex += helmets[uid].dex;
				playerOne.str += helmets[uid].str;
				playerOne.luk += helmets[uid].luk;
				
				inv_default.Delete(uid);
				
				break;
			case 2:
				cout << "No other type of items";
				break;
			case 3:
				cout << "No other type of items";
				break;
			default:
				break;
		}
	}
	
	
}*/


I need to use the helmets array in my main file along with using it in the function that is commented out.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Simple C++ Question...

Post by xiphirx »

ADCoffee wrote:Thanks for the reply.

I think I didn't explain myself fully. I'll just copy the code directly from my class's files. (The stuff I'm having trouble with is commented out.)

class header file:

Code: Select all

#ifndef armor
#define
 
#include <string>

using namespace std;

class armor {
public:
	string name;
	int type;
	int def;
	int dex;
	int str;
	int luk;
	int aid;
	//void useitem(int);
	static size_t count;
	static size_t howMany()
    { return count; }
	
	
	void puton(int);
	armor(string, int, int, int, int, int);
	armor();
	
};

#endif

class file:

Code: Select all

#include "Armor.h"
#include "Player.h"
#include <string>

using namespace std;

//armor helmets[300];

armor::armor(std::string n, int t, int df, int dx, int sr, int lk) {
	count++;
	name = n;
	type = t;
	def = df;
	dex = dx;
	str = sr;
	luk = lk;
	aid = 100 + (count % 100);
	
	/*helmets[aid].name = n;
	helmets[aid].def = df;
	helmets[aid].dex = dx;
	helmets[aid].str = sr;
	helmets[aid].luk = lk;*/
}

armor::armor() {
	count++;
	name = "Temp";
	type = 3;
	def = 1;
	dex = 0;
	aid = 100 + count;
}

/*void armor::useitem(int uid) {
	//int xx = 0;
	//for (int x = 0; x < armor::howMany() ; x++) {
	
	if (inv_default.search(uid) == true) {
		
		int typelo = uid % 100;
		
		if (playerOne.currentHelm > 100) {
			inv_default.Add(playerOne.currentHelm);
			if (helmets[playerOne.currentHelm].def > 0)
				playerOne.def -= helmets[playerOne.currentHelm].def;
			if (helmets[playerOne.currentHelm].dex > 0)
				playerOne.dex -= helmets[playerOne.currentHelm].dex;
			if (helmets[playerOne.currentHelm].str > 0)
				playerOne.str -= helmets[playerOne.currentHelm].str;
			if (helmets[playerOne.currentHelm].luk > 0)
				playerOne.luk -= helmets[playerOne.currentHelm].luk;
			
		}
		
		
		switch ((uid-typelo)/100) {
			case 1:
				playerOne.currentHelm = uid;
				playerOne.def += helmets[uid].def;
				playerOne.dex += helmets[uid].dex;
				playerOne.str += helmets[uid].str;
				playerOne.luk += helmets[uid].luk;
				
				inv_default.Delete(uid);
				
				break;
			case 2:
				cout << "No other type of items";
				break;
			case 3:
				cout << "No other type of items";
				break;
			default:
				break;
		}
	}
	
	
}*/


I need to use the helmets array in my main file along with using it in the function that is commented out.
I see many errors, for one, making all of your class data public is not a good idea ;)

Error:
this is in your class definition "void useitem(int);"
this is what you are using "void armor::useitem(int uid)"
you didnt specify the argument "uid" in the definition ;p

Error:
armor helmets[300];
1) I don't see why this is declared in your implementation file...
2) you did not overload the operator(s) [ and ], so you cannot use [300], instead you want to use your constructor like so:
armor helmets(300);

this will not work:
/*helmets[aid].name = n;
helmets[aid].def = df;
helmets[aid].dex = dx;
helmets[aid].str = sr;
helmets[aid].luk = lk;*/
because [ and ] are not overloaded.

I think you are misinterpreting your own class...

since it seems like you want to hold an array of items, and the armor class is the item object, it seems like you need another class, "armorController" to keep a list of all of the armor. The controller class will have a dynamic array (vectors, wee) that will be defined with the datatype "armor".


If you are new to C++, I highly recommend reading more on classes.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
ADCoffee
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 3
Joined: Sat Apr 17, 2010 10:25 pm

Re: Simple C++ Question...

Post by ADCoffee »

Oh...
It worked before I moved the classes to there own files.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Simple C++ Question...

Post by xiphirx »

ADCoffee wrote:Oh...
It worked before I moved the classes to there own files.
If it worked before, it may be the very slim chance of success, or I am a very dumb guy ;)
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Simple C++ Question...

Post by MrDeathNote »

xiphirx wrote: Error:
this is in your class definition "void useitem(int);"
this is what you are using "void armor::useitem(int uid)"
you didnt specify the argument "uid" in the definition ;p
What??? That is not an error, you know that you don't have to specify a variable name in the prototype right??? You only need to specify a type, he was completely right to do that if he wants to.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Simple C++ Question...

Post by hurstshifter »

AD, I think you might be better off instantiating the array of Armor objects somewhere in your main.cpp file and just pass a reference to the array into any of the armor functions that require its information. That way you can access the array in other sections of the game loop easily.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Simple C++ Question...

Post by xiphirx »

MrDeathNote wrote:
xiphirx wrote: Error:
this is in your class definition "void useitem(int);"
this is what you are using "void armor::useitem(int uid)"
you didnt specify the argument "uid" in the definition ;p
What??? That is not an error, you know that you don't have to specify a variable name in the prototype right??? You only need to specify a type, he was completely right to do that if he wants to.
xiphirx wrote:
ADCoffee wrote:Oh...
It worked before I moved the classes to there own files.
If it worked before, it may be the very slim chance of success, or I am a very dumb guy ;)
:o
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Simple C++ Question...

Post by MrDeathNote »

hurstshifter wrote:AD, I think you might be better off instantiating the array of Armor objects somewhere in your main.cpp file and just pass a reference to the array into any of the armor functions that require its information. That way you can access the array in other sections of the game loop easily.
Yea i'd say this is a sensible way to handle this.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
dream_coder
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Sat Mar 27, 2010 5:16 pm

Re: Simple C++ Question...

Post by dream_coder »

I just been reading about vectors. They seem better than arrays.
Image
Image
Image
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: Simple C++ Question...

Post by hurstshifter »

dream_coder wrote:I just been reading about vectors. They seem better than arrays.
'Better' probably isn't the best way to put it but they can certainly be tremendously useful in the right situation. Sometimes an array is all you need especially when the amount of objects/variables within it is not going to change.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Simple C++ Question...

Post by MrDeathNote »

dream_coder wrote:I just been reading about vectors. They seem better than arrays.
It depends what your using them for they each have pros and cons. There is an overhead with vectors that arrays dont have, but its only noticeable if you have shit loads of vectors. Vectors are great because they're dynamic, but like i say it depends what you need them for.
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply