1

I have been converting our software to use the string table so we can start to role out multiple languages. Generally going though and making sure all hardcoded strings are now loaded in from the string table. It was going swimmingly!

However, I have come up against this code and have been getting lots of compiler errors trying to convert between CString and char[]:

struct UnitDetails
{
    char Description[50] ;
    COLORREF Colour ;
    long UnitLength ; // In OneTenthMS
} ;

UnitDetails UDetails[ TIME_UNIT_COUNT ] =
{
    {"Hrs", HOURS_TREND_DISPLAY_COL  , OneHourInTenthMilliSeconds },
    {"Mins", MINUTES_TREND_DISPLAY_COL, OneMinuteInTenthMilliSeconds }, 
    {"Secs", SECONDS_TREND_DISPLAY_COL, OneSecInTenthMilliSeconds }
} ;

CTrendDisplay::Method(CDC* pDC)
{
    [...]
    pDC->DrawText( UDetails[j1].Description, &r, DT_RIGHT ) ; 
}

However, after various efforts I tried to change the code to this:

struct UnitDetails
{
    CString Description ;
    COLORREF Colour ;
    long UnitLength ; // In OneTenthMS
} ;

CString sHrs(MAKEINTRESOURCE(IDS_HOURS));
CString sMins(MAKEINTRESOURCE(IDS_MINUTES));
CString sSecs(MAKEINTRESOURCE(IDS_SECONDS));

UnitDetails UDetails[ TIME_UNIT_COUNT ] =
{
    {sHrs, HOURS_TREND_DISPLAY_COL  , OneHourInTenthMilliSeconds },
    {sMins, MINUTES_TREND_DISPLAY_COL, OneMinuteInTenthMilliSeconds }, 
    {sSecs, SECONDS_TREND_DISPLAY_COL, OneSecInTenthMilliSeconds }
} ;


CTrendDisplay::Method(CDC* pDC)
{
    [...]
    pDC->DrawText( (LPCTSTR)(UDetails[j1].Description), &r, DT_RIGHT ) ; 
}

and got the following compiler error:

error C2440: 'initializing' : cannot convert from 'class CString' to 'struct UnitDetails'

Without making this post super long and boring I've tried many other work arounds but keep getting stumped.

Does anyone have an insights that could bring a fresh perspective?

Thanks,

Matt

1 Answer 1

1

Since CString is a class, and implements a constructor, you'll have to implement a ctor for your UnitDetails.

Like this example:

struct UnitDetails
{
    CString Description;
    int     Colour;
    UnitDetails(const CString &s, int i): Description(s), Colour(i) {}
};

And initialize the array like this:

UnitDetails UDetails[] = {UnitDetails("foo", 1), UnitDetails("bar", 2)};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.