+ Reply to Thread
Results 1 to 2 of 2

Thread: I get "cannot access private member declared in class" error, what is this?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    46

    I get "cannot access private member declared in class" error, what is this?

    Code:
    class CBase1
    {
    public:
    	CBase1() {}
    private:
    	CBase1(int x){}
    } ;
    
    class CDerive1 : public CBase1
    {
    public:
    	CDerive1(int x) : CBase1(x) {} 
    } ;
    
    void Funct()
    {
    	CDerive1 obj(1) ;
    }
    What am I missing in above code?

    It says: error C2248: 'CBase1::CBase1' : cannot access private member declared in class 'CBase1'.

    What is this error about?

  2. #2
    Cause of the issue is that you have declare one of the constructor of the base class as 'private'.

    Derive class tries to access it but it cannot!

    Below code should resolve the issue:

    Code:
    class CBase1
    {
    public:
    	CBase1() {}
    	CBase1(int x){} //Note: Remove private from here...
    } ;
    
    class CDerive1 : public CBase1
    {
    public:
    	CDerive1(int x) : CBase1(x) {} 
    } ;
    
    void Funct()
    {
    	CDerive1 obj(1) ;
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts