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", 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?



-------------Problems Reply------------

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)};

Category:visual c++ Views:0 Time:2009-10-20
Tags: visual c++

Related post

  • Howto initialise char array style string in constructor 2008-10-29

    I have a class which I'm serialising to send over a unix socket and it has to have a string which I've stored as a char array. Can I initialise it in the constructor differently to how I've done it here? typedef struct SerialFunctionStatus_t { Serial

  • Why is an initialiser containing a string literal valid to initialise a `char` array? 2011-11-09

    It turns out that char c[] = {"a"}; is completely valid in both C++03 and C++11. I would not expect it to be, because it is an array of char not of char const*, and I would expect a brace-initialiser to require a compatible type for each of its "item

  • 2 Dimensional Char Array to CStrings, where each row is a separate string 2011-12-23

    I'm reading a multi-dimensional char array from a file char pszBillToAddress[3][31]; Each row of this array holds a line of an address, and ultimately I need to separate all of the components into separate strings for Address, City, State, and Zip, b

  • How to convert a C string (char array) into a Python string when there are non-ASCII characters in the string? 2008-10-17

    I have embedded a Python interpreter in a C program. Suppose the C program reads some bytes from a file into a char array and learns (somehow) that the bytes represent text with a certain encoding (e.g., ISO 8859-1, Windows-1252, or UTF-8). How do I

  • In C - check if a char exists in a char array 2009-07-01

    I'm trying to check if a character belongs to a list/array of invalid characters. Coming from a Python background, I used to be able to just say : for c in string: if c in invalid_characters: #do stuff, etc How can I do this with regular C char array

  • Char array in a struct - incompatible assignment? 2009-08-18

    I tried to find out what a struct really 'is' and hit a problem, so I have really 2 questions: 1) What is saved in 'sara'? Is it a pointer to the first element of the struct? 2) The more interesting question: Why doesn't it compile? GCC says "test.c:

  • is there a way to use cin.getline() without having to define a char array size before hand? 2010-04-02

    Basically my task is having to sort a bunch of strings of variable length ignoring case. I understand there is a function strcasecmp() that compares cstrings, but doesn't work on strings. Right now I'm using getline() for strings so I can just read i

  • stack dump accessing malloc char array 2010-05-12

    gcc 4.4.3 c89 I have the following source code. And getting a stack dump on the printf. char **devices; devices = malloc(10 * sizeof(char*)); strcpy(devices[0], "smxxxx1"); printf("[ %s ]\n", devices[0]); /* Stack dump trying to print */ I am thinkin

  • Array initialisation warning 2010-06-08

    I'm trying to initialise a structure which ends with an array[0] (here, char iedata[0]) for the actual packet payload. If I try to initialise it inline, like this: struct some_packet pkt = { .elem1 = blah, .elem2 = bleh, .iedata = { 1, 2, 3, 4 } }; I

  • How to turn a hex string into an unsigned char array? 2010-07-10

    For example, I have a cstring "E8 48 D8 FF FF 8B 0D" (including spaces) which needs to be converted into the equivalent unsigned char array {0xE8,0x48,0xD8,0xFF,0xFF,0x8B,0x0D}. What's an efficient way to do this? Thanks! EDIT: I can't used the std l

  • Count number of chars in char array including spaces until null char 2010-09-23

    I'm trying to count the number of chars in a char array including the space until the end of the string. The following compiles but doesn't return the correct value, I'm trying to use pointer arithmetic to interate through my array. int numberOfChars

  • Does a char array need to be one byte bigger than you intend to use? - C 2011-02-18

    I've started learning C and am a bit confused when it comes to arrays. #include <stdio.h> int main() { int i; char j[5]; for (i = 0; i < 5; i++) { j[i] = 'a'; } printf("%s\n", j); } Running this code prints out aaaaa♣ I've read that the char

  • Program aborts when using strcpy on a char pointer? (Works fine on char array) 2011-04-13

    I'm perplexed as to why the following doesn't work: char * f = "abcdef"; strcpy(f, "abcdef"); printf("%s",f); char s[] = "ddd"; strcpy(&s[0], "eee"); printf("%s", s); In both examples strcpy received a char * yet on the first example it dies a ho

  • convert VARIANT to unsigned char array 2011-05-12

    I'm using Comm32 (an activeX control) to get data coming through serial port using the method GetInput(). This method returns a _variant_t, so I need to convert it to an array of unsigned char's. When serial port is configured to TextMode (this is da

  • Copy char array to another char array 2011-06-24

    I'm trying without success to copy a char array to another one. I have tried memcpy copying direcly the address from one to another, like this: void include(int id, char name[16]) { int i; for (i = 0; i < SZ; i++) { if (a[i].id == 0) { a[i].id = i

  • char arrays length in C 2011-06-27

    In C, you have to declare the length of an array: int myArray[100]; But when you're dealing with chars and strings, the length can be left blank: char myString[] = "Hello, World!"; Does the compiler generate the length for you by looking at the strin

  • Converting Char array to Long in C 2011-07-14

    This question may looks silly, but please guide me I have a function to convert long data to char array void ConvertLongToChar(char *pSrc, char *pDest) { pDest[0] = pSrc[0]; pDest[1] = pSrc[1]; pDest[2] = pSrc[2]; pDest[3] = pSrc[3]; } And I call the

  • Scanf erases a char array unwillingly 2011-07-22

    See the following program: #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h> main(void){ printf("Array concatenateor\n-------------------------\n"); //declarations char s1[50],s2[50],s3[50]; short i

  • Char array sequence error 2011-07-31

    I'm having trouble storing a char sequence array of a defined length in to an object of a struct. I can make it work without defining a char length or just using a string but it just bothers me why this is happening. The code: #include <iostream

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.141 (s). 11 q(s)