10 Replies - 1476 Views - Last Post: 27 October 2006 - 01:23 PM Rate Topic: -----

#1 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Access data from a class instance

Post icon  Posted 23 October 2006 - 11:23 AM

Hello, I need some help accessing data from an instance of a class. The class is being used to control which directions a player can travel in. (It's a text based game.)

I've shortened the code I've written to just the class, instance, and function with the if statement that's calling the data from the class instance.

#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class room //The class for all the diffrent rooms in the game
{
public:
int level;//Shows which level the room is in.
int type;//Filled with water, or fire, or other obstacal that requires a magical cape to get by.
bool north;//The directions that the player can travel in.
bool south;
bool east;
bool west;
bool teleport;//Allows player to teleport to levels that he has been to.
bool nextFloor;//Route to the next level.
bool start;//the starting location for the level.
bool monster1;//Monsters of lowest levelThe game randomly chooses between the monsters from 1 of three diffrent levels.
bool monster2;//Monsters of medium level
bool monster3;//Monsters of high level
}


//The function that currently only tells you if you can travel west or not. (Will be expanded later when it starts working.)
//This is where the data from the class instance is being called at
int castleRoom(){
if (startTest::west == true){cout<<'You can travel west.';}
else {cout<<'You can't travel west';}
};


int main(int argc, char *argv[])
{

// This is the instance that the function is calling data from
room startTest; // The instance from the class 'room', it has the characteristics of the starting room.
startTest.level = 1;
startTest.type = 1;
startTest.north = false;
startTest.south = false;
startTest.east = false;
startTest.west = true;
startTest.teleport = false;
startTest.nextFloor = false;
startTest.start = true;
startTest.monster1 = false;
startTest.monster2 = false;
startTest.monster3 = false;



castleRoom(); //Calls the function


system('PAUSE');
return 0;
}


There also seem to be some other problems in the code that I havn't gotton out yet. Or maybe it's relevant to the problem that I'm asking about.

Is This A Good Question/Topic? 0
  • +

Replies To: Access data from a class instance

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Access data from a class instance

Posted 23 October 2006 - 11:40 AM

There are a few areas to address with this, including the fact that your castleRoom method is not scoped as a member of the class itself. As such, it will need to be provided the instance of the room object in order to access it's members. Check the following:
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

class room //The class for all the diffrent rooms in the game
{
public:
int level;//Shows which level the room is in.
int type;//Filled with water, or fire, or other obstacal that requires a magical cape to get by.
bool north;//The directions that the player can travel in.
bool south;
bool east;
bool west;
bool teleport;//Allows player to teleport to levels that he has been to.
bool nextFloor;//Route to the next level.
bool start;//the starting location for the level.
bool monster1;//Monsters of lowest levelThe game randomly chooses between the monsters from 1 of three diffrent levels.
bool monster2;//Monsters of medium level
bool monster3;//Monsters of high level
};


//The function that currently only tells you if you can travel west or not. (Will be expanded later when it starts working.)
//This is where the data from the class instance is being called at
int castleRoom(room startTest){
if (startTest.west == true){cout<<"You can travel west.";}
else {cout<<"You can't travel west";}
}



int main(int argc, char *argv[])
{

// This is the instance that the function is calling data from
room startTest; // The instance from the class 'room', it has the characteristics of the starting room.
startTest.level = 1;
startTest.type = 1;
startTest.north = false;
startTest.south = false;
startTest.east = false;
startTest.west = true;
startTest.teleport = false;
startTest.nextFloor = false;
startTest.start = true;
startTest.monster1 = false;
startTest.monster2 = false;
startTest.monster3 = false;



castleRoom(startTest); //Calls the function


return 0;
}


This is a simple debug using many assumptions on my part that are likely incorrect. Did you wish to make the castleRoom function a method of the room class? If so, we'll need to change the scoping and syntax.
Was This Post Helpful? 0
  • +
  • -

#3 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: Access data from a class instance

Posted 23 October 2006 - 12:01 PM

The purpose of the function is to control what happens in the diffrent rooms, like wheather or not you can travel to the north or west or get to the next level or if there's an item or monster in the room or something. And it would be deciding on most of that based on the data of an instance.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Access data from a class instance

Posted 23 October 2006 - 12:05 PM

But did you want to make it part of the class, or have it be an external function?
Was This Post Helpful? 0
  • +
  • -

#5 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: Access data from a class instance

Posted 23 October 2006 - 12:08 PM

What diffrences are there?
Was This Post Helpful? 0
  • +
  • -

#6 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Access data from a class instance

Posted 24 October 2006 - 04:28 AM

View PostThrack256, on 23 Oct, 2006 - 12:08 PM, said:

What diffrences are there?

Thrack (G'day armadeus)

Armadeus is pointing out that access to class members can be restricted by the use of private or protected.

In such cases ( and its the right way to manage class internal data) you must make code that must access the data a member too.

Castleroom is a function that cannot access the interior of your room.

either:

create functions to enable access or

make castleroom a member.
Was This Post Helpful? 0
  • +
  • -

#7 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: Access data from a class instance

Posted 24 October 2006 - 07:38 AM

I'm going to have the function access another class too, so I guess I should have functions to make the two classes accesible to the castleroom function. The other class would be for monsters, their health, attack, level (low, medium, and high levels), and I was thinking I'd allow some monsters to be able to use magical attacks. (Like stun or bind to keep the player from attacking or leaving.)
(Maybe I should rename the function to roomController or something similar.)
Was This Post Helpful? 0
  • +
  • -

#8 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Access data from a class instance

Posted 25 October 2006 - 01:58 AM

View PostThrack256, on 24 Oct, 2006 - 07:38 AM, said:

I'm going to have the function access another class too, so I guess I should have functions to make the two classes accesible to the castleroom function. The other class would be for monsters, their health, attack, level (low, medium, and high levels), and I was thinking I'd allow some monsters to be able to use magical attacks. (Like stun or bind to keep the player from attacking or leaving.)
(Maybe I should rename the function to roomController or something similar.)

In that case, I believe that the function will need to be a "friend" function, which grants access to non-member functions.
Was This Post Helpful? 0
  • +
  • -

#9 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: Access data from a class instance

Posted 25 October 2006 - 05:04 AM

How do those functions work? How are they written? And does the non-member function just call the member function? Maybe telling it what it wants.
Was This Post Helpful? 0
  • +
  • -

#10 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Access data from a class instance

Posted 25 October 2006 - 06:02 AM

View PostThrack256, on 25 Oct, 2006 - 05:04 AM, said:

How do those functions work? How are they written? And does the non-member function just call the member function? Maybe telling it what it wants.


Try looking up the free e-book - Thinking in C++ by Bruce Eckel.

His book is about the best explanation on the topic I can think of
Was This Post Helpful? 0
  • +
  • -

#11 Thrack256  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-October 06

Re: Access data from a class instance

Posted 27 October 2006 - 01:23 PM

The forum apparently stopped emailing me about posts, annoying when that happens.

I think I downloaded Thinking in C++ but I lost a bunch of stuff that was on my computer recently (well, everything actually, except what I happened to have on my other computer) and Thinking in C++ was lost. So I guess I'll just find it again.
And I'll need member function that can be accessed by the external functions, right? It's called a member function?

Thanks for the information.
Found the website. I guess I'll just download both volumes.
http://www.mindview....ingInCPP2e.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1