Why do I keep getting errors with the string?

Garrett Tiller 6 Reputation points
2023-06-10T21:55:33.1833333+00:00

I'm building the UI for the health

   //New   -----------------------      
const wchar_std::wstring output = std::wstring(L"Player-Health:") + std::wstring(m_player->health);

This std::wstring(m_player->health) has this error:

Error (active) E0289 no instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Alloc=std::allocator<wchar_t>]" matches the argument list "

Developer technologies C++
{count} votes

3 answers

Sort by: Most helpful
  1. David Lowndes 2,640 Reputation points MVP
    2023-06-10T22:37:02.03+00:00

    I assume that wchar_std::wstring is a typo and should be just std::wstring

    Other than that I guess that m_player->health is a std::string rather than a std::wstring.


  2. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-06-12T05:32:21.0033333+00:00

    Hi,

    You could use to_wstring

    	const std::wstring output = std::wstring(L"Player-Health:") + std::to_wstring(m_player->health);
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Giovanni Dicanio 160 Reputation points
    2023-06-12T16:06:33.7066667+00:00

    The following part of the error message: "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Alloc=std::allocator<wchar_t>]"

    is basically a very verbose way to identify std::wstring. It's kind of "std::wstring as seen from the C++ compiler's glasses".

    In simple terms, the C++ compiler with that error message is basically telling you that it can't find a proper std::wstring constructor to build a std::wstring instance from the m_player->health.

    So, the question now is: What is the type of m_player->health ?

    If it's a numeric value like an int, or float, or double, etc. you may simply invoke std::to_wstring to convert it to a std::wstring instance.

    If it's an instance of std::string, you should clarify what encoding is used to represent the text, and properly convert from that encoding to Unicode UTF-16 to store the text in std::wstring. To do that, you can invoke the MultiByteToWideChar API, or reuse some available open-source Unicode conversion code.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.