Can't intialize C++ Struct with CString in it??
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 37

Thread: Can't intialize C++ Struct with CString in it??

  1. #1
    Join Date
    Jun 2004
    Posts
    1,329

    Can't intialize C++ Struct with CString in it??

    Since CString is part of MFC I think this is the right forum.

    I'm trying to compile the following and it doesn't work? How can I get the CString to intialize.


    Code:
    struct print
        {
        int  x; //row
        int  y; //col
        int  fid;
        CString *data;
        char *format;
        };
    
    
       struct print form [] =
       {
    	   { 30, 40, 1,"Name", "N/A" },
    	   { 30, 42, 1,"Address", "N/A" },
    	   { 30, 44, 1,"City State, Zipcode", "N/A" },
    	   { 30, 50, 1,"Vehicle", "N/A" },
        };
    the char *format is not a problem,

    but compiler doesn't like CString *data;

    tried CSting data and that also doesn't work,

    typecasting didn't work either?

    Any suggestions?
    Rate this post if it helped you.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,528

    Re: Can't intialize C++ Struct with CString in it??

    Well, what prevents you from implementing constructor in your struct?
    And not use CStringArray (or std::vector of CString objects) instead of CString*?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    Code:
    struct print
        {
        int  x; //row
        int  y; //col
        int  fid;
        CString *data;
        char *format;
        };
    
       struct print form [] =
       {
    	   { 30, 40, 1,"Name", "N/A" },
    	   { 30, 42, 1,"Address", "N/A" },
    	   { 30, 44, 1,"City State, Zipcode", "N/A" },
    	   { 30, 50, 1,"Vehicle", "N/A" },
        };
    The struct keyword is not necessary in C++ to define a variable of type 'print'.
    What you are trying to do here is to convert a const char[] to a CString*. That's not possible, because these are unrelated types. A CString* must point to a CString. In your code you did not instantiate CString, so there is nothing to point to.
    Quote Originally Posted by ADSOFT View Post
    tried CSting data and that also doesn't work,
    If I compile that, I get an error like
    Code:
    error C2440: 'initializing' : cannot convert from 'const char [5]' to 'CString'
            Constructor for class 'ATL::CStringT<BaseType,StringTraits>' is declared 'explicit'
    That's pretty self-explanatory. The CString constructor that takes a const char* is declared explicit, so you cannot implicitly convert from a const char* to a CString. You need to explicitly create a CString in your initialization.
    Code:
    print form = { 30, 40, 1, CString("Name"), "N/A" };
    Note that if you have a unicode build, you need to pass CString a const wchar_t*, which you can do by prefixing the string literal with L. I.e.
    Code:
    print form = { 30, 40, 1, CString(L"Name"), "N/A" };
    If you need to support both unicode and MBCS builds, you can wrap the string literal with the _T() macro.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    D_Drmmr ,

    tried the following it didn't work either?

    print form = { 30, 40, 1, CString("Name"), "N/A" };

    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString data;

    char *data;
    };

    CString temp("Address");

    struct print form [] =
    {
    { 30, 40, 1,CString("Name"), "N/A" },
    { 30, 42, 1, temp , "N/A" },
    { 30, 44, 1, &temp, "N/A" },
    { 30, 50, 1, &temp, "N/A" },

    };



    Rate this post if it helped you.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,528

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    D_Drmmr ,

    tried the following it didn't work either?
    You seem to not read the answers, instead just to go on your "trial & error" method....
    Why in the world (of MFC!) do you need a plain array of your struct?
    Why do you still use a raw char *pointer?

    Get rid of the plain C types. You are in the world of C++!
    Victor Nijegorodov

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,203

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    D_Drmmr ,

    tried the following it didn't work either?
    That does work if you use temp instead of &temp. The compiler was pretty specific about what was wrong.

  7. #7
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    Victor

    because that's what the program calls for. I'm looking for answers not commentaries. You seem to want to redesign the program insteadof answering the questions.
    Rate this post if it helped you.

  8. #8
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    GCDEF,

    No it doesn't work. did you try to compile it, or is this something you made up?

    What does your compilier tell you?
    Rate this post if it helped you.

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    No it doesn't work. did you try to compile it, or is this something you made up?

    What does your compilier tell you?
    Why are you asking someone else these questions if you didn't answer them yourself?
    Really, it's not that hard to understand that you can do better than "it doesn't work". The only sensible response to that is "What doesn't work?". Does the code not compile? If so, what compiler errors are you getting?
    Instead of complaining about the type of response you are getting from experienced professionals who use their own time to try to help you, you should put in a little more effort yourself.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    D_Drmmr,


    ????, it's obvious that you don't know the answer.

    My question to you is why are you making recomendations that don't work. Again if you don't know the answer, which you should know if you are a proffesional, then don't respond.
    Rate this post if it helped you.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,528

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    Victor

    because that's what the program calls for. I'm looking for answers not commentaries. You seem to want to redesign the program insteadof answering the questions.
    I am answering your question which was:
    Quote Originally Posted by ADSOFT View Post
    Since CString is part of MFC I think this is the right forum.

    I'm trying to compile the following and it doesn't work? How can I get the CString to intialize.
    ...
    but compiler doesn't like CString *data;
    tried CSting data and that also doesn't work,

    typecasting didn't work either?

    Any suggestions?
    So my suggestion was to redesign your structure(s) because your initial design was wrong!

    BTW, what do you mean by "that's what the program calls for"? What for?

    PS: Dear ADSOFT, you are in CG almost 9 years, you have more that 1300 posts! So why does your recent posts look like the ones from a beginner that never coded before nor has read the Announcements?
    Victor Nijegorodov

  12. #12
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    Mr Victor,

    Well I'm not looking to redesign the program, I think you should learn to read, my question was:

    "How can I get the CString to intialize.", that's what I'm looking to do. If you don't know how, then your help is not neccessary. And if you are not going to take the time to copy my original code and compile it and THEN figure out how to intialize it then your help is not neccessary.

    Again, for those who are interested , I'm trying to get the structure to initialize the CString with either a string constant or another string.

    And Mr Victor, if it is such a basic question, why don't you have the answer, duhhhhh.
    Rate this post if it helped you.

  13. #13
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    Here is some more info:


    I'm getting the following message from the compiler:

    error C2440: 'initializing' : cannot convert from 'const int' to 'struct print'
    No constructor could take the source type, or constructor overload resolution was ambiguous

    Compiler doesn't specifically state which var in the struct is the problem, but it seems to be the CString, tried different ways to intialize it, all of which are defined in the CString constructor definitions get same compiler error for each structure member.



    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString data;

    char *format;
    };

    CString gSin ;
    CString CSZ("CityStZip");

    CString temp;



    struct print form [] =
    {
    { 30, 40, 1,data("test"), "N/A" },
    { 30, 42, 1,temp("TEST") , "N/A" },
    { 30, 44, 1,CString("data"), "N/A" },
    { 30, 50, 1,temp, "N/A" },
    { 30, 50, 1,CSZ, "N/A" },

    };
    Rate this post if it helped you.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,528

    Re: Can't intialize C++ Struct with CString in it??

    Quote Originally Posted by ADSOFT View Post
    Mr Victor,

    Well I'm not looking to redesign the program, I think you should learn to read, my question was:

    "How can I get the CString to intialize.", that's what I'm looking to do.
    Well, how could you "initialize" something that you do not have?
    There is no CString objects in your structure! There are only a pointer to CString!
    The answer for pointer is obvious: either you point it to some already existing object (with the lifetime being enough for your purposes) or you create a new object with new (or new[]) and initialize it.
    Both variants look not good, therefore I suggested you to implement CStringArray or std::vector instead. But if your faith does not allow zou to use container classes, then sorry!
    Victor Nijegorodov

  15. #15
    Join Date
    Jun 2004
    Posts
    1,329

    Re: Can't intialize C++ Struct with CString in it??

    Changed the code to the following


    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString data;

    char *format;
    };

    CString gSin ;
    CString CSZ("CityStZip");

    CString temp;

    // struct print form [4];

    struct print form [] =
    {
    { 30, 40, 1,data("test"), "N/A" },
    { 30, 42, 1,temp("TEST") , "N/A" },
    { 30, 44, 1,CString("data"), "N/A" },
    { 30, 50, 1,temp, "N/A" },
    { 30, 50, 1,CSZ, "N/A" },

    };




    tried many different ways to initalize the CString all of which are okay with the Constructor definitions of CString.


    I'm getting the following compiler errors:

    error C2440: 'initializing' : cannot convert from 'const int' to 'struct print'
    No constructor could take the source type, or constructor overload resolution was ambiguous

    Pretty sure it's talking about the CString
    Rate this post if it helped you.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center