We help IT Professionals succeed at work.

declaring and changing variables in a mfc app...

zeugje
zeugje asked
on
592 Views
Last Modified: 2013-11-20
This question is a lot like:
http://oldlook.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20882944.html
Here is what I did:
1/ I made a mfc dialog based application with visual c++ 6
2/I added a “child-dialog”, which is invoked when I press a button in my main dialog.
3/I added a CString in my main class:

class CKmgDlg : public CDialog // this is my main class
{
      // Construction
public:
       CString aa;
4/under the about-box I made a button, and added this code:
void CAboutDlg::OnOK()
{
      // TODO: Add extra validation here
      //AfxGetMainWnd()->aa =5;
      ((CKmgDlg *)AfxGetMainWnd())->aa =" test_text ";

      CDialog::OnOK();
}
This code works fine and compiles without errors.
5/under the child-dialog’s OK button I added this code:
void CDia::OnOK()
{
      // TODO: Add extra validation here
      ((CKmgDlg *)AfxGetMainWnd())->aa ="test_text";


      CDialog::OnOK();
}
This gives the following errors:
error C2065: 'CKmgDlg' : undeclared identifier
error C2059: syntax error : ')'
when I add this code:

      CKmgDlg * mainWnd = (CKmgDlg *)AfxGetMainWnd();
    mainWnd->aa =5;
I have this error:
error C2065: 'CKmgDlg' : undeclared identifier

My Question is now:
1/how can I change the value off aa when I press the child’s OK button ??
2/can this also be done when I define aa in the child window and I want to change it when I press a button in the main window ??

Many thanks.


Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You need to '#include' the header file where 'CKmgDlg' is declared.

Author

Commented:
it says:
error C2007: #define syntax
(I thought that visual studio does this by himself ?!?)

Author

Commented:
sorry, did define and not include...

Author

Commented:
thanks for your comments,
but it gives again errors:
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or type specifiers
MsgBx.cpp(135) : error C2065: 'send_sock' : undeclared identifier

(where CLIENT_0_1Dlg is my mainwindow,
'm_msg' is the variable in CLIENT_0_1Dlg  of type CMsgBx (and set as private),
'CMsgBx' is my child-window,
'send_sock' is a variable from CLIENT_0_1Dlg which I want to use in  my function void "CMsgBx::OnMsgsend()" )


Author

Commented:

after I added the ((CKmgDlg *)AfxGetMainWnd())->, it gives only the first 3 errors; which are all on line 56.
Here is line 56:

class CCLIENT_0_1Dlg : public CDialog
{
// Construction
public:
....
private:
      CMsgBx m_msg;    //<- line 56
};

Commented:
What is CMsgBx? You need to add #include for it also.

Commented:
'CMsgBx' is my child-window. where should I include the header fiel ? And (for the future) how should I know where to include which header ?

Commented:
I think I added the includes on the right place , but I have still the errors on line 56 (see above, please)

Commented:
// CCLIENT_0_1Dlg.h
#include "MsgBox.h"

class CCLIENT_0_1Dlg : public CDialog
{
// Construction
public:
....
private:
     CMsgBx m_msg;    //<- line 56
};

Other way: in any cpp file, where you have
#include "CCLIENT_0_1Dlg."

put line
#include "MsgBox.h"

before it.
The rule is: C++ compiler should know every name in the code. In every place where you have class name, appropriate class file header should be included before.

Commented:
many thanks. But if I have included the headers,so that he "knows" the classes, how comes he don't seems to "know" m_msg, which is in the class (-> so what should I do about the
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or type specifiers
errors ??)

Commented:
If you added all required #include lines, possibly CMsgBx is wrong name (typo)?
Show your h and cpp files (relevant fragments).

Commented:
// CLIENT_0_1.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "CLIENT_0_1.h"
#include "CLIENT_0_1Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////
// CLIENT_0_1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "CLIENT_0_1.h"
#include "CLIENT_0_1Dlg.h"
#include "MsgBx.h"      // Added by ClassView
#include "CLIENT_0_1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int m_runFunction=0;
///////////////////////
// MsgBx.cpp : implementation file
//

#include "stdafx.h"
#include "CLIENT_0_1.h"
#include "MsgBx.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////////////////
// CLIENT_0_1Dlg.h : header file
//

#if !defined(AFX_CLIENT_0_1DLG_H__E60D9F19_7810_4669_A7B8_943C8CDD6B50__INCLUDED_)
#define AFX_CLIENT_0_1DLG_H__E60D9F19_7810_4669_A7B8_943C8CDD6B50__INCLUDED_

#include "MsgBx.h"      // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CCLIENT_0_1Dlg dialog

class CCLIENT_0_1Dlg : public CDialog
{
// Construction
public:
      SOCKET send_sock;
      CString CStmp;
      SOCKADDR_IN addr;
      CCLIENT_0_1Dlg(CWnd* pParent = NULL);      // standard constructor

// Dialog Data
      //{{AFX_DATA(CCLIENT_0_1Dlg)
      enum { IDD = IDD_CLIENT_0_1_DIALOG };
      CButton      m_bgo;
      CTreeCtrl      m_Tree;
      CIPAddressCtrl      m_ip;
      CString      m_mainedit;
      //}}AFX_DATA

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CCLIENT_0_1Dlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);      // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      HICON m_hIcon;

      // Generated message map functions
      //{{AFX_MSG(CCLIENT_0_1Dlg)
      virtual BOOL OnInitDialog();
      afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg void OnConnect();
      afx_msg void OnExit();
      afx_msg void OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult);
      afx_msg void OnGo();
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
private:
            CMsgBx m_msg;    //<-- this is the line which causes the errors, according to the compiler

};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CLIENT_0_1DLG_H__E60D9F19_7810_4669_A7B8_943C8CDD6B50__INCLUDED_)

////////////////////////////
// MsgBx.h : header file
#if !defined(AFX_MSGBX_H__47C25741_AB9B_4772_A217_D5923DC1F382__INCLUDED_)
#define AFX_MSGBX_H__47C25741_AB9B_4772_A217_D5923DC1F382__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// MsgBx.h : header file
//
#include "CLIENT_0_1Dlg.h"

//////////////////////////////

m_msg is the name of my MsgBx
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
that gives even more errors...

CLIENT_0_1Dlg.cpp
CLIENT_0_1Dlg.h(26) : error C2065: 'IDD_CLIENT_0_1_DIALOG' : undeclared identifier
CLIENT_0_1Dlg.h(26) : error C2057: expected constant expression
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or type specifiers
MsgBx.h(21) : error C2065: 'IDD_DMSGBOX' : undeclared identifier
MsgBx.h(21) : error C2057: expected constant expression
CLIENT_0_1Dlg.cpp(307) : error C2065: 'm_msg' : undeclared identifier
CLIENT_0_1Dlg.cpp(307) : error C2228: left of '.DoModal' must have class/struct/union type
MsgBx.cpp
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or

Commented:
#include "stdafx.h"
#include "CLIENT_0_1.h"
#include "MsgBx.h"     // Added by ClassView
#include "CLIENT_0_1Dlg.h"

Commented:
again errors...in the same files...

CLIENT_0_1Dlg.cpp
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or type specifiers
CLIENT_0_1Dlg.cpp(308) : error C2065: 'm_msg' : undeclared identifier
CLIENT_0_1Dlg.cpp(308) : error C2228: left of '.DoModal' must have class/struct/union type
MsgBx.cpp
CLIENT_0_1Dlg.h(56) : error C2146: syntax error : missing ';' before identifier 'm_msg'
CLIENT_0_1Dlg.h(56) : error C2501: 'CMsgBx' : missing storage-class or type specifiers
CLIENT_0_1Dlg.h(56) : error C2501: 'm_msg' : missing storage-class or type specifiers

Commented:
////////////////////////////
// MsgBx.h : header file
#if !defined(AFX_MSGBX_H__47C25741_AB9B_4772_A217_D5923DC1F382__INCLUDED_)
#define AFX_MSGBX_H__47C25741_AB9B_4772_A217_D5923DC1F382__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// MsgBx.h : header file
//
#include "CLIENT_0_1Dlg.h"        // remove this!!!

Author

Commented:
thanks. It works indeed.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.