accessing private

Started by
16 comments, last by Aldacron 18 years, 10 months ago
Hello guys I am having a problem getting this program to access a file in one of my header files for example: #ifndef ROOMS_H #define ROOMS_H class rooms { public: rooms(); void initializeRooms(); private: char name[32]; char description[500]; int north; int south; int east; int west; }; #endif [code/] It can not access the name array that is private. #include <cstdlib> #include <iostream> #include <ctime> #include <cctype> #include "equipment.h" #include "monster.h" #include "player.h" #include "rooms.h" using namespace std; void initializeBattle(); void giveItem(); int randomNumber(int); int main() { rooms roomsArray[11]; equipment equipmentArray[9]; player character; monster monsterArray[6]; equipment e; monster m; player p; rooms r; e.initializeEquipment(); m.initializeMonsters(); p.initializePlayer(); r.initializeRooms(); char move; int currentroom = 0; cout << "You emerge in the " <<roomsArray[currentroom].name; cout << roomsArray[currentroom].description; [code/]
Computers are worlds of Exploration?
Advertisement
What's your question?

And by the way, the end code tag is . And don't use that, use the [ source ] [/ source] tags instead.
Well of course it can't - that's the definition of it being private. Either make it public, or provide a public function to retrieve the value.

Also, consider using std::string instead of that character array garbage.
Hello guys I am having a problem getting this program to access a file in one of my header files for example:

#ifndef ROOMS_H#define ROOMS_Hclass rooms{public:rooms();void initializeRooms();private:char name[32];char description[500]; int north;int south;int east;int west;};#endif



It can not access the name array that is private.


#include <cstdlib>#include <iostream>#include <ctime>#include <cctype>#include "equipment.h"#include "monster.h"#include "player.h"#include "rooms.h"using namespace std;void initializeBattle();void giveItem();int randomNumber(int);int main(){rooms roomsArray[11];equipment equipmentArray[9];player character;monster monsterArray[6];equipment e;monster m;player p;rooms r;e.initializeEquipment();m.initializeMonsters();p.initializePlayer();r.initializeRooms();char move;int currentroom = 0;cout << "You emerge in the " <<roomsArray[currentroom].name;cout << roomsArray[currentroom].description;



My question is in main you notice that roomsArray[currentroom].name;
How can I get roomsArray to access name[32] in the rooms header file?
Computers are worlds of Exploration?
I would retype what I said, but you can just re-read it :)
Quote:Original post by RDragon1
I would retype what I said, but you can just re-read it :)


And next time, please EDIT your post instead of reposting it. And please read what he said.
Quote:Original post by C_Programmer0101
Hello guys I am having a problem getting this program to access a file in one of my header files for example:
*** Source Snippet Removed ***
It can not access the name array that is private.
*** Source Snippet Removed ***
My question is in main you notice that roomsArray[currentroom].name;
How can I get roomsArray to access name[32] in the rooms header file?


What you need to do is create a set of accessors for this class, as was mentioned by RDragon1. For example:

class rooms{public:    const char* GetName() { return name; };    const char* GetDescription( return description; );};


So now when you need to access it

cout << "You emerge in the " <<roomsArray[currentroom].GetName();cout << roomsArray[currentroom].GetDescription();


Then that's it! Just from looking at your code, I would reccomend you take a look into using something such as std::string rather than char[]'s simply because of the possibilities for overflows when you are working with this project as well as for the suggestion that RDragon1 provided. Just a suggestion....Good luck! [smile]
Ok thanks alot guys for you help!!! =)
Computers are worlds of Exploration?
void equipment::initializeEquipment(){ strcpy(equipmentArray[0].type, "Gun"); strcpy(equipmentArray[0].name, "Berreta"); equipmentArray[0].bonus = 10; strcpy(equipmentArray[1].type, "Gun"); strcpy(equipmentArray[1].name, "M4 Carbine Rifle"); equipmentArray[1].bonus = 30; strcpy(equipmentArray[2].type, "Gun"); strcpy(equipmentArray[2].name, "Flamethrower"); equipmentArray[2].bonus = 50; strcpy(equipmentArray[3].type, "Shield"); strcpy(equipmentArray[3].name, "Homemade wooden sheild"); equipmentArray[3].bonus = 10; strcpy(equipmentArray[4].type, "Shield"); strcpy(equipmentArray[4].name, "Roman sheild"); equipmentArray[4].bonus = 30; strcpy(equipmentArray[5].type, "Shield"); strcpy(equipmentArray[5].name, "Heavenly Protection"); equipmentArray[5].bonus = 50; strcpy(equipmentArray[6].type, "Armour"); strcpy(equipmentArray[6].name, "Flak Jacket"); equipmentArray[6].bonus = 10; strcpy(equipmentArray[7].type, "Armour"); strcpy(equipmentArray[7].name, "Kevlar SWAT vest"); equipmentArray[7].bonus = 10; strcpy(equipmentArray[8].type, "Gun"); strcpy(equipmentArray[8].name, "Possesed Armour"); equipmentArray[8].bonus = 10;} void equipment::giveItem(){	 int randomgenerator = 0;	 srand(static_cast<unsigned>(time(0)));	 randomgenerator = (rand()%9 + 1);	 char playerchoice;	 if (randomgenerator == 0 || randomgenerator == 1 || randomgenerator == 2)	 {		cout << "Do you want to pick up the " << equipmentArray[randomgenerator].name << " ?" << endl;		cout << "This will add " << equipmentArray[randomgenerator].bonus << "to your strength." << endl;		cout << "(Y)es or (N)o." << endl;		cin >> playerchoice;		playerchoice = toupper(playerchoice);	   if (playerchoice == 'Y')		   character.weapon = equipmentArray[randomgenerator].bonus;	   else		   character.weapon = character.weapon;	   }	 else if (randomgenerator == 3 || randomgenerator == 4 || randomgenerator == 5)	 {		cout << "Do you want to pick up the " << equipmentArray[randomgenerator].name << " ?" << endl;		cout << "This will add " << equipmentArray[randomgenerator].bonus << "to your dexterity." << endl;		cout << "(Y)es or (N)o." << endl;		cin >> playerchoice;		playerchoice = toupper(playerchoice);		if (playerchoice == 'Y')		   character.shield = equipmentArray[randomgenerator].bonus;		else			character.shield = character.shield;		}	 else if (randomgenerator == 6 || randomgenerator == 7 || randomgenerator == 8)	 {		cout << "Do you want to pick up the " << equipmentArray[randomgenerator].name << " ?" << endl;		cout << "This will add " << equipmentArray[randomgenerator].bonus << "to your dexterity." << endl;		cout << "(Y)es or (N)o." << endl;		cin >> playerchoice;		playerchoice = toupper(playerchoice);		if (playerchoice == 'Y')		   character.armour = equipmentArray[randomgenerator].bonus;		else			character.armour = character.shield;		}}    


New error and it is saying that:

16 E:\Documents and Settings\Owner\Desktop\Protocadaryn\equipment.cpp
request for member `type' in `
17 E:\Documents and Settings\Owner\Desktop\Protocadaryn\equipment.cpp
request for member `name' in `
18 E:\Documents and Settings\Owner\Desktop\Protocadaryn\equipment.cpp
request for member `bonus' in `


Can anyone help I am getting so close?
Computers are worlds of Exploration?
What IDE are you using? It should have some help in which you can find exactly what does your error message mean.
And it would certainly help if you posted source for the equipment class.

And as someone said, use std::string instead of char*. Then you won't need strcpy and it will look much better and be much more safe.

This topic is closed to new replies.

Advertisement