Page 1 of 1

Simple C++ Question...

Posted: Sat Apr 17, 2010 10:47 pm
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

Re: Simple C++ Question...

Posted: Sat Apr 17, 2010 11:16 pm
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.

Re: Simple C++ Question...

Posted: Sat Apr 17, 2010 11:25 pm
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.

Re: Simple C++ Question...

Posted: Sat Apr 17, 2010 11:40 pm
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.

Re: Simple C++ Question...

Posted: Sat Apr 17, 2010 11:45 pm
by ADCoffee
Oh...
It worked before I moved the classes to there own files.

Re: Simple C++ Question...

Posted: Sat Apr 17, 2010 11:47 pm
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 ;)

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 4:30 am
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.

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 9:34 am
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.

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 1:32 pm
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

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 1:46 pm
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.

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 2:07 pm
by dream_coder
I just been reading about vectors. They seem better than arrays.

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 2:53 pm
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.

Re: Simple C++ Question...

Posted: Sun Apr 18, 2010 3:07 pm
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.