+ Reply to Thread
Results 1 to 2 of 2

Thread: What is error: cannot access public member declared in base class?

  1. #1

    What is error: cannot access public member declared in base class?

    Below code does not compile:

    Code:
    class CMyBase
    {
    public:
    	int GetData() { return 100 ; } 
    } ;
    
    class CMyDerived : CMyBase
    {
    } ;
    
    void UseInheritance()
    {
    	CMyDerived objMyDerived ;
    	int nValue = objMyDerived.GetData() ;
    }
    What is the issue?

    Compiler error is : error C2248: 'GetData' : cannot access public member declared in class 'CMyBase'

    Help

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    46
    Looking at your code, you have forgotten access specifier while defining your derive class.

    Use below modified code:

    Code:
    class CMyBase
    {
    public:
    	int GetData() { return 100 ; } 
    } ;
    
    class CMyDerived : public CMyBase
    {
    } ;
    
    void UseInheritance()
    {
    	CMyDerived objMyDerived ;
    	int nValue = objMyDerived.GetData() ;
    }
    Note that we are using public while deriving CMyBase class!

    This is important.

    If you do not use this then default access specifier is private and that was the reason that you received that error from compiler.

    Hope this helps.

+ 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