By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Accessing member data from another class with pointers

P: n/a
Hey - i am working on a project and have encountered a problem.

when i try to make a pointer in an object to hold the address of
another object it works fine - but the problem is when i try to access
data through that pointer - my code is as follows:

// Cunit 2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream.h>

class Cweapon
{
public:

int itsDamage;
int itsRange;
int itsRoF;

};
class Cmember
{
public:
int itsHealth;
int itsSID;
};

class Cstat
{
public:
int itsMaxHealth;
int itsArmour_Expl,
itsArmour_Proj,
itsArmour_Blad;
int itsLoS;
int itsSpeed;
int itsSize;
int itsAccuracy;
int itsMaxMembers;
int itsValue;
};

class Cunit
{
public:
Cstat* itsStat;
Cweapon* itsWeapon;
Cmember itsMember[3];
int itsUID;
};

Cunit Utroop[10];
Cstat Stroop;
Cweapon Wtroop;
int Utroop_ctr = 0;

int main()
{
Stroop.itsMaxHealth = 100;
Stroop.itsArmour_Expl = 100;
Stroop.itsArmour_Proj = 100;
Stroop.itsArmour_Blad = 100;
Stroop.itsLoS = 100;
Stroop.itsSpeed = 100;
Stroop.itsSize = 100;
Stroop.itsAccuracy = 100;
Stroop.itsMaxMembers = 5;
Stroop.itsValue = 1000;

Wtroop.itsDamage = 100;
Wtroop.itsRoF = 100;
Wtroop.itsRange = 100;
return 0;
// if (User clicks on "create TROOP"button)

Utroop[Utroop_ctr].itsStat = &Stroop;
Utroop[Utroop_ctr].itsWeapon = &Wtroop;
Utroop[Utroop_ctr].itsUID = Utroop[Utroop_ctr].itsStat.itsValue +
Utroop_ctr;

for (int i = 0;i < Utroop[Utroop_ctr].itsStat.itsMaxMembers;i++) //
this is where probs occur
Utroop[Utroop_ctr].itsMember[i].itsHealth =
Utroop[Utroop_ctr].itsStat.itsMaxHealth;

Utroop_ctr++;

return 0;
}

//////////////code dump//////////////////

/*

*/

i would appreciate it if anyone could help me out here
Xpy

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
// Utroop[Utroop_ctr].itsUID = Utroop[Utroop_ctr].itsStat.itsValue +
Utroop_ctr;

itsStat is a pointer to the object. the correct way of accessing its
members would be...

Utroop[Utroop_ctr].itsUID = (Utroop[Utroop_ctr].itsStat)->itsValue +
Utroop_ctr;

the same problem exists in the for loop also... hope that would solve
the problem...

regards,
srini

Jul 23 '05 #2

P: n/a
ok, thank you very much

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.