nasc
nasc

Reputation: 299

How to reference/use the variables initialized by the constructor in other methods in a mfc application

So, like the title said I can't seem to use the variables initialized in the constructor in the mfc application.

// CMFCApplication1View construction/destruction

CMFCApplication1View::CMFCApplication1View() noexcept
{
    // TODO: add construction code here
    int x1 = 0;
}

but when I use those variables in the onDraw method it gives the undefined error

void CMFCApplication1View::OnDraw(CDC* pDC)
{   
    CMFCApplication1Doc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    pDC->Rectangle(20+x1, 30, 100+x1, 120);  //<- error here
}

Edit:
Ok so the way I asked the question was completely wrong, what I actually wanted to ask was how to declare member variables in an MFC application, (specifically the CMFCApplication1View.cpp file), that I can use in other member functions of the same class.

Upvotes: 0

Views: 83

Answers (1)

Joseph Willcoxson
Joseph Willcoxson

Reputation: 6040

You probably have a file like MFCApplication1View.h. Your class will be declared in that file or a similarly named file. Find the class declaration in the header file and modify like this:

class CMFCApplication1View
{
   // ... other stuff

   int x1;
};

You can even initialize it in the header file instead of the constructor (this is an alternate to the above, not an addition):

class CMFCApplication1View
{
   // ... other stuff

   int x1 = 0;
};

Upvotes: 1

Related Questions