c++mfccedit

How to display std::string into CEdit in C++


I have a MFC project written in C++ and I want to display some text in a CEdit control. So far, I tried this:

MFCApplication1Dlg.h

...

private:
    static CEdit m_progress;
public:
    static void setProgress(std::string message);

...

MFCApplication1Dlg.cpp

...

void CMFCApplication1Dlg::setProgress(std::string message)
{
    m_progress.SetWindowTextW((LPCTSTR)message.c_str());
}

void logMessage(std::string message)
{
    if(logFile.is_open())
    {
        logFile << message;
        logFile.flush();
        CMFCApplication1Dlg::setProgress(message);

    }
}

...

When I compile I have this error:

error LNK2001: unresolved external symbol "private: static class CEdit CMFCApplication1Dlg::m_progress" (?m_progress@CMFCApplication1Dlg@@0VCEdit@@A)

Can anyone tell me what is with this error and how could I display the messages in that CEdit?


Solution

  • You have to define the static member in your cpp file:

    CEdit CMFCApplication1Dlg::m_progress;
    

    In the header it is just declared, not defined.

    Also, I would expect SetWindowTextW to take a wstring::c_str() parameter, and not a string::c_str().