CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 37

Hybrid View

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

    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
    Hanover Germany
    Posts
    20,395

    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,042

    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,352

    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
    Hanover Germany
    Posts
    20,395

    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
    Join Date
    Jun 2004
    Posts
    1,352

    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.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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

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

    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.

  9. #9
    Join Date
    Jun 2004
    Posts
    1,352

    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.

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

    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.

  11. #11
    Join Date
    Jun 2004
    Posts
    1,352

    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.

  12. #12
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    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

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

    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.

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

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

    Quote Originally Posted by ADSOFT View Post
    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?
    My compiler told me your first two worked, but the second two it couldn't convert a CString* to a CString. When I removed the &, it compiled cleanly. You may want to dump the attitude. Sheesh. This compiled cleanly.

    Code:
    #include "stdafx.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif
    
    struct print
        {
        int  x; //row
        int  y; //col
        int  fid;
        CString data;
        char *format;
        };
    
    
    // The one and only application object
    
    CWinApp theApp;
    
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    	int nRetCode = 0;
    
     CString temp("Address");
    
     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" },
    
    };
    
    	// initialize MFC and print and error on failure
    	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    	{
    		// TODO: change error code to suit your needs
    		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
    		nRetCode = 1;
    	}
    	else
    	{
    		// TODO: code your application's behavior here.
    	}
    
    	return nRetCode;
    }
    Last edited by GCDEF; April 9th, 2013 at 05:38 AM.

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

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

    GCDEF,

    In my case I defined the struct "print form[]" globally in a FORMVIEW app, in you case you defined it local to "_tmain".

    I tried sticking your example inside a my FormView::OnPrint function, still wouldn't compile. Getting

    error C2440: 'initializing' : cannot convert from 'const int' to 'struct print'
    No constructor could take the source type, or constructor overload resolution was ambiguous
    Rate this post if it helped you.

Page 1 of 2 12 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
  •  





Click Here to Expand Forum to Full Width

Featured