zeugje
asked on
error C2597: illegal reference to data member 'CAaaDlg::aa' in a static member function
How can I acces variables (public) from class_A out of class_B ?
I thought it was like that:
---------------------
class class_A: public CDialog
{
// Construction
public:
int aa;
.....
---------------------
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
class_A::aa =5;
CDialog::OnOK();
}
---------------------
but it always gives "error C2597: illegal reference to data member 'CAaaDlg::aa' in a static member function" as an error.
I thought it was like that:
---------------------
class class_A: public CDialog
{
// Construction
public:
int aa;
.....
---------------------
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
class_A::aa =5;
CDialog::OnOK();
}
---------------------
but it always gives "error C2597: illegal reference to data member 'CAaaDlg::aa' in a static member function" as an error.
ASKER
thanks,but how should I find the name of the instantiating object of my main window(class), using Visual c++6.0. ?
(I mean: I have a dialog application with a start window which calls an "option-window", I gave the option window a name->so I know that one, but I didn't made a class or gave a name to the main window.)
(I mean: I have a dialog application with a start window which calls an "option-window", I gave the option window a name->so I know that one, but I didn't made a class or gave a name to the main window.)
What about AfxGetMainWnd() for the window handle and AfxGetApp() for the CWinApp derived application object ?
ASKER
How should I applie that on the class_a class_b example? (ps: I'm new to programming)
You have a dialog based MFC project. If the main dialog window definition starts like this:
class MainDlg : public CDialog
{
// Construction
public:
int aa;
.....
And the about box has a OnOK function that needs to find the name of the instantiating object of your main window then you could do this:
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
AfxGetMAinWnd()->aa =5;
CDialog::OnOK();
}
This works because the main dialog window has already been instantiated. If you want to set the value of a variable in a class that has not been instantiated then you need to instantiate the object before you set the value, OR make the variable static like this:
class class_A: public CDialog
{
// Construction
public:
static int aa;
.....
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
class_A::aa =5;
CDialog::OnOK();
}
which works fine as long as somewhere you create the static variable (just putting the definition in the class doesn't do that) So, in some .cpp file you would also need (at file scope):
class_A::aa =0;
class MainDlg : public CDialog
{
// Construction
public:
int aa;
.....
And the about box has a OnOK function that needs to find the name of the instantiating object of your main window then you could do this:
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
AfxGetMAinWnd()->aa =5;
CDialog::OnOK();
}
This works because the main dialog window has already been instantiated. If you want to set the value of a variable in a class that has not been instantiated then you need to instantiate the object before you set the value, OR make the variable static like this:
class class_A: public CDialog
{
// Construction
public:
static int aa;
.....
void CAboutDlg::OnOK()
{
// TODO: Add extra validation here
class_A::aa =5;
CDialog::OnOK();
}
which works fine as long as somewhere you create the static variable (just putting the definition in the class doesn't do that) So, in some .cpp file you would also need (at file scope):
class_A::aa =0;
ASKER
When I trie the first, It gives the following errors.
error C2065: 'AfxGetMAinWnd' : undeclared identifier
error C2227: left of '->aa' must point to class/struct/union
Do you happen to know how this can be solved?
(if you want extra points,please say it, because I don't know enough of programming to know if it's a difficult/ normal / easy question)
error C2065: 'AfxGetMAinWnd' : undeclared identifier
error C2227: left of '->aa' must point to class/struct/union
Do you happen to know how this can be solved?
(if you want extra points,please say it, because I don't know enough of programming to know if it's a difficult/ normal / easy question)
Yikes! Did I write that? Try AfxGetMainWnd() instead. Here is what the MSDN says about it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_afxgetmainwnd.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_afxgetmainwnd.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot. Now it works indeed.
{
class_A objA;
objA.aa = 5;
}