Accessing member data from another class with pointers

X

Xpyder

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.itsHealth =
Utroop[Utroop_ctr].itsStat.itsMaxHealth;

Utroop_ctr++;

return 0;
}

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

/*

*/

i would appreciate it if anyone could help me out here


Xpy
 
N

None

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top