Accessing Objects within another object


I'm currently trying to access a variable contained within a base object from another completely different object and I continually get error messages. I'll attempt to put up the important code since it's contained within fairly large classes.

From the Base .cpp file. ObjectPosition:

1
2
3
4
5
6
7
8
9
10
11
12
void ObjectPosition::init(float x,float y, float speed, int boundx, int boundy, float hit, int lives, bool live)
{
ObjectPosition::x = x;
ObjectPosition::y = y;
ObjectPosition::speed = speed;
ObjectPosition::boundx = boundx;
ObjectPosition::boundy = boundy;
ObjectPosition::hit = hit;
ObjectPosition::lives = lives;
ObjectPosition::live = live;

}


This is the initialization function for the BaseObject. All objects are inheriting these variables which are Protected within the ObjectPosition class.

Then they are initialized within the Pig class thus wise:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void Pig::binit(float sx,float sy, ALLEGRO_BITMAP *simage)
{


	//Sets all ObjectPosition Variables
	ObjectPosition::init(800,900,10,80,40,40,10,true);

		smaxFrame = 4;
		scurFrame = 0;
		sframeCount = 0;
		sframeDelay = 2;
		sframeWidth = 250;
		sframeHeight = 140;
		sanimationColumns = 4;
		sanimationWalk = 0;
		sanimationRows = 2;
		sanimationDirection = 1;
		sdirectionChange = 0;



		simage = simage;
	//Bear::TakeLife = &TakeLife;
}


So far so good all variables are being inherited fine. Pig functions correctly and so does the Bear.

Ok Now hear is where I'm having my issue. I have a separate class for the Player who interacts with the Pig to have an armor when activated sets the Pig's boundx to 0. Whenever I place it within the function I get an error. I have a bear written as a struct which works fine. And I can access the Object Position boundx with no problem, but that doesn't effect the pigs boundx.

Here's the function where I want the Pigs boundx set to 0. It's written within the Armor.h/.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Armor::Collided(Dave &warrior, Bear bear[], Directions &direction, Pig *aPig)
{
	
	{
		
		if(slive == true)
		{
		bear[1].boundx = 0;
		aPig->pinitx();
		samurai.armor--;

		}
		else if (slive == false)
		bear[1].boundx = 200;
		warrior.armor = 5;
	}

}


I tried to initialize the boundx through the pig via pinitx but I get errors and I can't access through the pig to the object position to the boundx.

The warrior is written as a struct works fine.
http://www.cplusplus.com/forum/articles/40071/#msg216270


http://www.eelis.net/iso-c++/testcase.xhtml
Make sure your testcase is self-contained and actually reproduces the problem

A testcase is said to reproduce the problem if, when we independently try to compile and run the testcase locally, we get the same compiler error (or our compiler's version of it) or erroneous program output specified in the testcase. A testcase that does not reproduce the problem is useless.

A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem


Last edited on
Here's the error message:

1>------ Build started: Project: Allegro test project, Configuration: Debug Win32 ------
1> Armor.cpp
1>c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\armor.cpp(73): error C2248: 'ObjectPosition::boundx' : cannot access protected member declared in class 'ObjectPosition'
1> c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\objectposition.h(15) : see declaration of 'ObjectPosition::boundx'
1> c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\objectposition.h(9) : see declaration of 'ObjectPosition'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
That's when I have the function work as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Armor::Collided(Dave &warrior, Bear bear[], Directions &direction, Pig *aPig)
{
	
	{
		
		if(slive == true)
		{
		bear[1].boundx = 0;
		aPig->boundx;
		warrior.armor--;

		}
		else if (slive == false)
		bear[1].boundx = 200;
		warrior.armor = 5;
}
}


I can't imagine how to create a test case since it's cross 5 different cpp/h files. If someone could walk me through it that would help.
Or if I do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void Armor::Collided(Dave &warrior, Bear bear[], Directions &direction, Pig *aPig)
{
	
	{
		
		if(slive == true)
		{
		bear[1].boundx = 0;
		Pig::ObjectPosition::boundx = 0;
		warrior.armor--;

		}
		else if (slive == false)
		bear[1].boundx = 200;
		warrior.armor = 5;
	}

}


I get this error

1>------ Build started: Project: Allegro test project, Configuration: Debug Win32 ------
1>  Armor.cpp
1>c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\armor.cpp(73): error C2597: illegal reference to non-static member 'ObjectPosition::boundx'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So in other words you have:

1
2
3
4
5
6
7
8
9
10
class Pig
{
    int boundx;
};

Class Armor
{
    public:
        void Collided( /*stuff*/ Pig *aPig );
};


Then you are trying to access the private of a Pig inside of Armor class. Like:

1
2
3
4
void Armor::Collided( /*stuff*/ Pig *aPig )
{
    std::cout << aPig->boundx; //Error: boundx is private not public
}


The solution would probably be to make boundx public or to have a getter function. (depending on if you are modifying or not it could look different)

1
2
const int* GetBoundX( void ) const; //const pointer to const object
int* GetBoundY( void ); //non-const pointer to non-const object 


http://stackoverflow.com/questions/856542/elegant-solution-to-duplicate-const-and-non-const-getters

*added link
Last edited on
Ok I'm going to try to create a getter function to see if that works. Basically what your saying is I should be "getting" the bound x from Pig. Set it to a place holder like obBoundx. Then I should be "setting" it with another function.

The getter and setter should be in public Pig and not public object postion because they are being initialized in private Pig. Or do they need to be in object Position?
You can use the constructor instead of a setter. But if you want to know where it is for your collision you are going to need to get the position from the object. If the location is private only the methods of the class may access it not the methods of another class. Also when I looked back it looked like you tried to make it protected to do this. Protected pretty much means that the derived class methods can access the protected variables of another class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class A
{
    public:
        const int *GetFoo( void ) const { return &foo; }
    protected:
        int foo;
};

class B : public A
{
    B(){ std::cout << foo << std::endl; } 
    //works you are directly accessing the protected

    //B( const A &a ){ std::cout << a.foo << std::endl; }
    //error: foo is not public so foo can not accessed outside of methods

    B( const A &a ) { std::cout << *a.GetFoo() << std::endl; } 
    //works GetFoo is a public and may be accessed anywhere (including
    //outside of method)
};
So I tried creating a getter function but it wont do anything. Then I realized i didnt set it to the Pig contructor. So I did and now I'm getting an error.

Here's the code:
1
2
3
4
5
int Pig:: Getpigspeed()
{
	return 40;
	std::cout<<3;
}


here's the error.
1>------ Build started: Project: Allegro test project, Configuration: Debug Win32 ------
1>  Pig.cpp
1>c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\pig.cpp(169): error C2084: function 'int Pig::Getpigspeed(void)' already has a body
1>          c:\users\amarthy\documents\visual studio 2010\projects\allegro test project\allegro test project\pig.h(43) : see previous definition of 'Getpigspeed'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Again, useless.
The error seems to imply that you've got
1
2
3
4
5
6
7
8
9
class Pig{
   int Getpidspeed(){ /**/ } //defining the function
};

int Pig:: Getpigspeed() //defining the function again
{
	return 40;
	std::cout<<3;
}
but that's just a guess.


> I can't imagine how to create a test case since it's cross 5 different cpp/h files
upload it in https://github.com/ or similar.
however, you should try to isolate the problem.

If you have compilation error, simply post that cpp and whatever .h that it includes
I put the variable in the public part of the class and it works. How come if I try to access it through the a getter function nothing happens?

Also, can you have a class inherit from two classes?
Topic archived. No new replies allowed.