6 Replies - 22158 Views - Last Post: 15 December 2008 - 03:38 PM Rate Topic: -----

#1 newjohnwu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 16-October 08

cannot access private member declared in class

Post icon  Posted 15 December 2008 - 08:57 AM

i'm not sure y this is happen
i keep getting this error and not sure what i have to do.

cpp(37) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'
cpp(41) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'

mainly this problem
 b=n 

AND
 int b; 


here's the whole thing

#include <iostream>
using namespace std;

class B
{
	public:
		B();
		B(int n);
		void print()const;

	private:
		int b;
};
B::B()
{
	b=0;
}

B::B(int n)
	{
		cout<<"B:"<<b<<"/n";
	}

	class D: public B
	{
	public:
		D();
		D(int);
		void print()const;
	};

	D::D()
	{
	}
	D::D(int n)
	{
		b=n;
	}
	void D::print() const
	{
		cout<<"D:"<<b<<"/n";
	}



Is This A Good Question/Topic? 0
  • +

Replies To: cannot access private member declared in class

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5411
  • View blog
  • Posts: 27,416
  • Joined: 10-May 07

Re: cannot access private member declared in class

Posted 15 December 2008 - 08:59 AM

** Renamed title to reflect question **
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: cannot access private member declared in class

Posted 15 December 2008 - 09:16 AM

Private members are private, even to their children. If you want to share that member variable with a child class, you need to expose it through a method or make the member variable itself visible through using protected specification rather than private.
Was This Post Helpful? 0
  • +
  • -

#4 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: cannot access private member declared in class

Posted 15 December 2008 - 09:21 AM

View Postnewjohnwu, on 15 Dec, 2008 - 02:57 PM, said:

i'm not sure y this is happen
i keep getting this error and not sure what i have to do.

cpp(37) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'
cpp(41) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'


See my comments on the same subject in this thread: http://www.dreaminco...wtopic76337.htm

Basically the member varaible has a private access rights, hence the error. You need to provide access, and better via getters and setters.

	class D: public B
	{
	public:
		D();
		D(int);
		void print()const;
	};

	D::D()
	{
	}
	D::D(int n)
	{
		b=n;
	}
	void D::print() const
	{
		cout<<"D:"<<b<<"/n";
	}



Seeing that D inherits from B then you need to set the member variables of B as protected NOT private. They will then be accessable by child classes that inherit from B etc... but still be private to outsiders.
Was This Post Helpful? 0
  • +
  • -

#5 cwginac  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 26
  • Joined: 13-December 08

Re: cannot access private member declared in class

Posted 15 December 2008 - 10:57 AM

*Edit, sorry didn't read all of the previous post.

This post has been edited by cwginac: 15 December 2008 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#6 siddude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-December 08

Re: cannot access private member declared in class

Posted 15 December 2008 - 11:20 AM

View Postnewjohnwu, on 15 Dec, 2008 - 07:57 AM, said:

i'm not sure y this is happen
i keep getting this error and not sure what i have to do.

cpp(37) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'
cpp(41) : error C2248: 'b' : cannot access private member declared in class 'B'
cpp(12) : see declaration of 'b'

mainly this problem
 b=n 

AND
 int b; 


here's the whole thing

#include <iostream>
using namespace std;

class B
{
	public:
		B();
		B(int n);
		void print()const;

	private:
		int b;
};
B::B()
{
	b=0;
}

B::B(int n)
	{
		cout<<"B:"<<b<<"/n";
	}

	class D: public B
	{
	public:
		D();
		D(int);
		void print()const;
	};

	D::D()
	{
	}
	D::D(int n)
	{
		b=n;
	}
	void D::print() const
	{
		cout<<"D:"<<b<<"/n";
	}




ok so ur logics are not clear with inheritence. When a class is inherited with public access ie


class a
{
int c;
private:
void a();
};

class b:public a
{....
};




everything inherited from class a is inherited as it is,function a is also inherited as private and not excessable outside the class a.
so to excess the function a or value c(which are all private)



class a
{
int c;
private:
void a();
public:
void d()
{
a();
}
};

class b:public a
{....
};




now function d is available in class b which can excess private member functions of a.


hope it helps u
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3124
  • View blog
  • Posts: 19,168
  • Joined: 14-September 07

Re: cannot access private member declared in class

Posted 15 December 2008 - 03:38 PM

In your example you should make d() protected if you only want derived classes to access it. Anything can access a public function or variable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1