Class access failed

Started by
4 comments, last by Enigma 18 years, 1 month ago
Hi all , i have a problem about accesing a class' member via pointer from another class .The code like that :

#include<iostream>
using namespace std ;
class One{
 public :
 One( int new_on = 10 ){
 on = new_on ; 
 }
 int getOn(){ return on ;}
private :
 int on ;
};
class Two{
 public :
 Two( int new_two = 20 ){
 tw = new_two ;
 }
private :
 int tw ;
 One *p_One ;
 
};
main(){
 One numberOne();
 Two numberTwo() ;
 // can i use like that ?
 cout << numberTwo.p_One->getOn() ;

}



I am a newbie in c++ , i have to use my class like that .If it cant be done like that ,is there anyone who know how i can access another class' member without inheritance. Thanks for all answers :)
Advertisement
"numberTwo.p_One" is private, so main() cannot access it. You can make it public or provide a getter method.
But if i want to use like that the same problem occurs
#include<iostream>using namespace std ;class One{ public : One( int new_on = 10 ){ on = new_on ;  } int getOn(){ return on ;}private : int on ;};class Two{ public : Two( int new_two = 20 ){ tw = new_two ; } void SetOneMember(){ p_One->on = 33 ; }private : int tw ; One *p_One ; };main(){ One numberOne(); Two numberTwo() ; // can i use like that ? cout << numberTwo.SetOneMember() ;}
Question: who is initializing p_One?

You must understand that a pointer just point to some memory location. It do not imply the existence of a pointed object.

Ie: to be valid, a pointer should reference a valid memory zone (you can allocate this memory zone using new of you can point to anotehr existing variable but in the end, you still have to reference an existing memory zone).
class Two{private:  One *mOne;public:  Two()  {    // allocate mOne:    mOne = new One();  }  virtual ~Two()  {    // since we allocated it, we should also destroy it    delete mOne;  }};

You should read again the section about pointer in your prefered C/C++ book [smile]

HTH,
thanks Emmanuel , i will help me :)
Two contains a pointer to a One, but you never actually set that pointer to point to anything. So when you try and use the pointer your program falls over (undefined behaviour).

There are also some additional errors in your code. The syntax One numberOne() does not define a variable of type One named numberOne, it declares a function named numberOne which takes no parameters and returns a One. To fix this remove the parentheses. They should only be used if the constructors takes at least one argument. In the latest version you're also trying to print out the result of calling a function which returns void. If a function returns void it means it doesn't return anything, so you don't have anything to print!

Try something like:
#include <cassert>#include <iostream>using namespace std;class One{	public:		One(int new_on = 10)		{			on = new_on;		}		int getOn()		{			return on;		}	private:		int on;};class Two{	public:		Two(One & one, int new_two = 20)		{			one_ = &one;			tw = new_two;		}		One * GetOneMember()		{			return one_;		}	private:		int tw;		One * one_;};int main(){	One numberOne;	Two numberTwo(numberOne);	// get used to using asserts.  This line ensures then the pointer	// returned from numberTwo is not null (a null pointer doesn't point to	// anything)	assert(numberTwo.GetOneMember());	cout << numberTwo.GetOneMember()->getOn() << '\n';}

Σnigma

This topic is closed to new replies.

Advertisement