Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error 2248: cannot access protected member declared in CWinapp

Error 2248: cannot access protected member declared in CWinapp

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++windows-admin
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eastman75
    wrote on last edited by
    #1

    I am new to C++ MFC programming and -- though I've read a lot concerning the subject -- none of recipes seems to work in my case. The snippet of code is as follows:

    void CPatchDlg::OnStart()
    {
    ...
    // Write the result into the Registry
    CWinApp* pApp = AfxGetApp() ;
    pApp->SetRegistryKey(lpszRegistryKey) ;
    }

    Function SetRegistryKey(lpszKey) is a protected member of CWinApp class and cannot be used directly:

    error C2248: 'SetRegistryKey' : cannot access protected member declared in class 'CWinApp'...

    Please, help me to work around the problem.

    A A 2 Replies Last reply
    0
    • E eastman75

      I am new to C++ MFC programming and -- though I've read a lot concerning the subject -- none of recipes seems to work in my case. The snippet of code is as follows:

      void CPatchDlg::OnStart()
      {
      ...
      // Write the result into the Registry
      CWinApp* pApp = AfxGetApp() ;
      pApp->SetRegistryKey(lpszRegistryKey) ;
      }

      Function SetRegistryKey(lpszKey) is a protected member of CWinApp class and cannot be used directly:

      error C2248: 'SetRegistryKey' : cannot access protected member declared in class 'CWinApp'...

      Please, help me to work around the problem.

      A Offline
      A Offline
      Abhi Lahare
      wrote on last edited by
      #2

      what you are trying to achieve?

      E 1 Reply Last reply
      0
      • E eastman75

        I am new to C++ MFC programming and -- though I've read a lot concerning the subject -- none of recipes seems to work in my case. The snippet of code is as follows:

        void CPatchDlg::OnStart()
        {
        ...
        // Write the result into the Registry
        CWinApp* pApp = AfxGetApp() ;
        pApp->SetRegistryKey(lpszRegistryKey) ;
        }

        Function SetRegistryKey(lpszKey) is a protected member of CWinApp class and cannot be used directly:

        error C2248: 'SetRegistryKey' : cannot access protected member declared in class 'CWinApp'...

        Please, help me to work around the problem.

        A Offline
        A Offline
        Albert Holguin
        wrote on last edited by
        #3

        Since its a protected member method, it can only be accessed from the CWinApp. Find your CWinApp derived class and you should be ale to make that call there.

        E 1 Reply Last reply
        0
        • A Albert Holguin

          Since its a protected member method, it can only be accessed from the CWinApp. Find your CWinApp derived class and you should be ale to make that call there.

          E Offline
          E Offline
          eastman75
          wrote on last edited by
          #4

          Sorry, Albert, I don't undersdand what you suggest. Suppose I derive an auxiliary class as follows:

          class CAux : public CWinApp
          {
          public:
          void SetRegistryKey(LPCTSTR lpszKey);
          };

          Then in some other place I make a call:

          CAux::SetRegistryKey("somekey");

          Trying to compile, I got the following message:

          error C2352: 'CAux::SetRegistryKey' : illegal call of non-static member function

          I don't understand how to avoid the access rights for the protected member function. Regards, Anatoly

          L A 2 Replies Last reply
          0
          • E eastman75

            Sorry, Albert, I don't undersdand what you suggest. Suppose I derive an auxiliary class as follows:

            class CAux : public CWinApp
            {
            public:
            void SetRegistryKey(LPCTSTR lpszKey);
            };

            Then in some other place I make a call:

            CAux::SetRegistryKey("somekey");

            Trying to compile, I got the following message:

            error C2352: 'CAux::SetRegistryKey' : illegal call of non-static member function

            I don't understand how to avoid the access rights for the protected member function. Regards, Anatoly

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            You don't need to redefine SetRegistryKey() in your derived class. Just code it as follows somewhere within one of your CAux class functions:

            SetRegistryKey("somekey");

            The best things in life are not things.

            1 Reply Last reply
            0
            • E eastman75

              Sorry, Albert, I don't undersdand what you suggest. Suppose I derive an auxiliary class as follows:

              class CAux : public CWinApp
              {
              public:
              void SetRegistryKey(LPCTSTR lpszKey);
              };

              Then in some other place I make a call:

              CAux::SetRegistryKey("somekey");

              Trying to compile, I got the following message:

              error C2352: 'CAux::SetRegistryKey' : illegal call of non-static member function

              I don't understand how to avoid the access rights for the protected member function. Regards, Anatoly

              A Offline
              A Offline
              Albert Holguin
              wrote on last edited by
              #6

              If you used the application wizard to start your application building, and you specified that you were building an MFC application... then you should already have a CWinApp derived class somewhere in your project. Don't redefine the call, just call it from somewhere in there (if you redefine you'll override the original call). And you can only the CAux::SetRegistryKey(); method without initializing an object if the call is static (something you don't want to do anyway).

              E 2 Replies Last reply
              0
              • A Albert Holguin

                If you used the application wizard to start your application building, and you specified that you were building an MFC application... then you should already have a CWinApp derived class somewhere in your project. Don't redefine the call, just call it from somewhere in there (if you redefine you'll override the original call). And you can only the CAux::SetRegistryKey(); method without initializing an object if the call is static (something you don't want to do anyway).

                E Offline
                E Offline
                eastman75
                wrote on last edited by
                #7

                Thanks, Albert, You are completely right and it was my fault. Indeed, my application has a CwinApp derived class which is called CPatchApp:

                class CPatchApp : public CWinApp
                {
                public:
                CPatchApp();
                ...
                }

                All I had to do then is to declare

                (CPatchApp *) pApp = AfxGetApp();

                and to use that pApp pointer to access protected SetRegistryKey function like:

                pApp->SetRegistryKey( LPCTSTR lpszRegistryKey );

                Instead I wrote

                (CWinApp *) pApp = AfxGetApp();

                1 Reply Last reply
                0
                • A Albert Holguin

                  If you used the application wizard to start your application building, and you specified that you were building an MFC application... then you should already have a CWinApp derived class somewhere in your project. Don't redefine the call, just call it from somewhere in there (if you redefine you'll override the original call). And you can only the CAux::SetRegistryKey(); method without initializing an object if the call is static (something you don't want to do anyway).

                  E Offline
                  E Offline
                  eastman75
                  wrote on last edited by
                  #8

                  Sorry, it doesn't work either. I do have a CWinApp derived class in my project (refer to my previous reply). It is a simple dialog application. I need to write to the registry some value and -- before using WriteProfileString -- I try to call SetRegistryKey. There is the code snippet:

                  AfxGetApp()->SetRegistryKey(lpszRegistryKey);
                  

                  At compiling I got the following message:

                  patchdlg.cpp(229) : error C2248: 'SetRegistryKey' : cannot access protected member declared in class 'CWinApp'

                  which returns us to the beginning of the discussion. Is it possible to avoid that error at all? What is it there that I have overlooked?

                  A 1 Reply Last reply
                  0
                  • E eastman75

                    Sorry, it doesn't work either. I do have a CWinApp derived class in my project (refer to my previous reply). It is a simple dialog application. I need to write to the registry some value and -- before using WriteProfileString -- I try to call SetRegistryKey. There is the code snippet:

                    AfxGetApp()->SetRegistryKey(lpszRegistryKey);
                    

                    At compiling I got the following message:

                    patchdlg.cpp(229) : error C2248: 'SetRegistryKey' : cannot access protected member declared in class 'CWinApp'

                    which returns us to the beginning of the discussion. Is it possible to avoid that error at all? What is it there that I have overlooked?

                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #9

                    Where are you making that call from? You have to make it from within the CWinApp derived class (since it is protected).

                    E 1 Reply Last reply
                    0
                    • A Albert Holguin

                      Where are you making that call from? You have to make it from within the CWinApp derived class (since it is protected).

                      E Offline
                      E Offline
                      eastman75
                      wrote on last edited by
                      #10

                      Hi, Albert, I'd tried various approaches until I found an excellent C++ Tutorial (http://www.learncpp.com/) and got the understanding that my problem is related to the inheritance subject. As it is clearly stated in the tutorial:

                      1. The protected access specifier allows derived classes to directly access members of the base class while not exposing those members to the public.

                      I see now that I have to make call within but my derived class. It does work except that there reveals another problem -- WriteRegisterString needs an access to the m_pszRegistryKey variable which is an attribute of CWinApp class. IAE, I have to learn a bit more on the subject. The discussion was very helpful and I thank you for the time you spent on it. Regards, Anatoly

                      1 Reply Last reply
                      0
                      • A Abhi Lahare

                        what you are trying to achieve?

                        E Offline
                        E Offline
                        eastman75
                        wrote on last edited by
                        #11

                        I've been trying to write into the registry some data not related to the application in case. As it happens CWinApp functions are not intended for such usage -- they are intended to write the registration data of the running application. In that case the correct usage is as follows: 1. Place call to the SetRegistryKey function in the "InitInstance". For example,

                        BOOL DerivedApp::InitInstance()
                        {
                        // Standard initialization

                        LPCTSTR lpszRegistryKey = "SomeSection" ;
                        SetRegistryKey(lpszRegistryKey) ;
                        . . .
                        

                        }

                        2. Place call to the other necessary functions in the proper place of your application. For example,

                        void SomeClass::OnStart()
                        {
                        . . .

                        LPCTSTR lpszSection    = "SomeSection" ;
                        LPCTSTR lpszStringItem = "SomeString" ;
                            LPCTSTR lpszValue      = "SomeStringValue" ;
                        if (!AfxGetApp()->;WriteProfileString(lpszSection, lpszStringItem, lpszValue))
                        	AfxMessageBox("Registry settings weren't modified.") ;
                            . . .
                        

                        }

                        I've checked it -- it works. But not in the case if you have to write into the Registry some data not related to your application. Regards to all of you, chaps!

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups