Problem with CString's Format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ProgMaster
    New Member
    • Jul 2007
    • 3

    Problem with CString's Format

    I've used the Cstring format to convernt a CTime into a a CString, yet I get an error message, here's the code:

    void CTimerDlg::OnTi mer(UINT_PTR nIDEvent)
    {
    CString m_sTime;
    // TODO: Add your message handler code here and/or call default
    CTime CurTime = CTime::GetCurre ntTime();

    m_sTime.Format ("%d,%d,%d", CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());

    UpdateData(FALS E);

    CDialog::OnTime r(nIDEvent);
    }

    The Error message is : error C2664: 'void ATL::CStringT<B aseType,StringT raits>::Format( const wchar_t *,...)' : cannot convert parameter 1 from 'const char [9]' to 'const wchar_t *'

    How do I do this?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by ProgMaster
    I've used the Cstring format to convernt a CTime into a a CString, yet I get an error message, here's the code:

    void CTimerDlg::OnTi mer(UINT_PTR nIDEvent)
    {
    CString m_sTime;
    // TODO: Add your message handler code here and/or call default
    CTime CurTime = CTime::GetCurre ntTime();

    m_sTime.Format ("%d,%d,%d", CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());

    UpdateData(FALS E);

    CDialog::OnTime r(nIDEvent);
    }

    The Error message is : error C2664: 'void ATL::CStringT<B aseType,StringT raits>::Format( const wchar_t *,...)' : cannot convert parameter 1 from 'const char [9]' to 'const wchar_t *'

    How do I do this?
    I think const wchar_t * typed parameter should be passed to CurTime.GetHour () function and you have passes const char [9] types parameter. Which causes such an error

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by Progmaster
      m_sTime.Format ("%d,%d,%d", CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());
      If you are using <tchar.h> this should be:
      [code=c]
      m_sTime.Format (TEXT("%d,%d,%d "), CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());

      Comment

      • ProgMaster
        New Member
        • Jul 2007
        • 3

        #4
        Originally posted by weaknessforcats
        If you are using <tchar.h> this should be:
        [code=c]
        m_sTime.Format (TEXT("%d,%d,%d "), CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());
        Thank you very much, the code now works perfectly, but could you please tell me what caused the error, becuase I took the code directly from a tutorial about Visual C++. And what does the command "TEXT(..)" do?

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by ProgMaster
          Thank you very much, the code now works perfectly, but could you please tell me what caused the error, becuase I took the code directly from a tutorial about Visual C++. And what does the command "TEXT(..)" do?
          TEXT is a macro. If _UNICIODE is defined TEXT becomes L:

          [code=c]
          m_sTime.Format (L"%d,%d,%d", CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());
          [/code]

          The L means the string is wchar_t characters and not char characters.

          If _UNICODE is not defined, the TEXT becomes nothing.

          This allows your code to switch between ASCII and UNICODE without having to change the code.

          There are lot of things to worry about. Start here.

          Comment

          • ProgMaster
            New Member
            • Jul 2007
            • 3

            #6
            Originally posted by weaknessforcats
            TEXT is a macro. If _UNICIODE is defined TEXT becomes L:

            [code=c]
            m_sTime.Format (L"%d,%d,%d", CurTime.GetHour (), CurTime.GetMinu te(),CurTime.Ge tSecond());
            [/code]

            The L means the string is wchar_t characters and not char characters.

            If _UNICODE is not defined, the TEXT becomes nothing.

            This allows your code to switch between ASCII and UNICODE without having to change the code.

            There are lot of things to worry about. Start here.
            Thank you very much, the page was very helpul. I'm still a beginner in C++ , and after programming Visual Basic for so long, many things in C++ seem very confusing.

            Comment

            Working...