CString.LoadString to Char Array Initialisation Outside of Class
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", MINU开发者_如何学GoTES_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
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)};
精彩评论