Error 2248: cannot access protected member declared in CWinapp
-
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.
-
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.
what you are trying to achieve?
-
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.
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.
-
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.
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
-
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
-
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
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).
-
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).
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();
-
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).
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?
-
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?
Where are you making that call from? You have to make it from within the CWinApp derived class (since it is protected).
-
Where are you making that call from? You have to make it from within the CWinApp derived class (since it is protected).
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:
- 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
-
what you are trying to achieve?
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 initializationLPCTSTR 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!