Jump to content
  • Advertisement
Sign in to follow this  
C_Programmer0101

accessing private

This topic is 4886 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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/]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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






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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites


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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!