0

I tried to shorten a foreign code. I thougth I could save one variable.

The following given code is OK and shows a Windows Frame.

#include <afxwin.h> 
// from source: http://www.codersource.net/2010/01/30/mfc-tutorial-part-1/
class MFC_Tutorial_Window :public CFrameWnd
{
public:
    MFC_Tutorial_Window()    
    {
        Create(NULL, "MFC Tutorial Part 1 CoderSource Window");  
    }
};

class MyApp :public CWinApp
{
    MFC_Tutorial_Window *wnd;  

public:
    BOOL InitInstance()
    {
        wnd = new MFC_Tutorial_Window();
        m_pMainWnd = wnd;                        
        m_pMainWnd->ShowWindow(1);
        return 1;
    }
};

MyApp theApp;

After my rewording it doesn`t function anymore. No build errors. But it doesn't show a frame.

#include <afxwin.h> 
// from source: http://www.codersource.net/2010/01/30/mfc-tutorial-part-1/
// and changed by me

class MFC_Tutorial_Window :public CFrameWnd
{
public:
    MFC_Tutorial_Window()
    {
        Create(NULL, "MFC Tutorial Part 1 CoderSource Window");
    }
};

class MyApp :public CWinApp
{
    // del MFC_Tutorial_Window *wnd;
    MFC_Tutorial_Window *m_pMainWnd; // ins 


public:
    BOOL InitInstance()
    {
        // del wnd = new MFC_Tutorial_Window();
        // del m_pMainWnd = wnd;
        m_pMainWnd = new MFC_Tutorial_Window(); // ins
        m_pMainWnd->ShowWindow(1);
        return 1;
    }
};

MyApp theApp;

What's the matter?

CarpeDiemKopi
  • 316
  • 3
  • 13
  • 1
    Dont you re-declare the variable `m_pMainWnd` ? I cant see its declaration in first code, so why are you creating member variable `m_pMainWnd` in second example? Just try to delete the row `MFC_Tutorial_Window *m_pMainWnd;` – kocica Aug 06 '17 at 17:32
  • Now it works. Thank you for the hint. Now I can understand this behaviour. – CarpeDiemKopi Aug 06 '17 at 17:44
  • Np mate, glad it helped. Just posted it to comments so you can mark this thread as anwered, regards :) – kocica Aug 06 '17 at 20:10
  • I believe this should issue a warning at warning level 4 (3 is the default in a wizard-generated project, unfortunately). – IInspectable Aug 08 '17 at 16:49

1 Answers1

1

The problem is in redeclaration of member variable MFC_Tutorial_Window *m_pMainWnd;, without this row it'll work.

kocica
  • 6,412
  • 2
  • 14
  • 35