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

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

  1. #16
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

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

    I'm not sure where the error message is coming form, but there are
    a couple of errors in your posted code.

    Code:
    struct print form [] =
    {
       { 30, 40, 1,data("test"), "N/A" },          // what is data ?
       { 30, 42, 1,temp("TEST") , "N/A" },         // temp is already constructed
       { 30, 44, 1,CString("data"), "N/A" },
       { 30, 50, 1,temp, "N/A" },
       { 30, 50, 1,CSZ, "N/A" },
    };

  2. #17
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,055

    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.

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

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

    Philip,


    data is

    CString data;

    and

    temp was already defined yes but I'm trying to initialize it to a string "TEST".

    thanks
    Rate this post if it helped you.

  4. #19
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,928

    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?
    Sure it didn't. Why does your structure has two members named data? Why you initialization list entries differ in passed types?

    Tried the following and it worked just fine.

    Code:
    #include <windows.h>
    #include <atlstr.h>
    
    #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
    
    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString sData;
    
    char *chData;
    };
    
    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" },
    
    };
    
    
    int main() 
    {
    	for (int idx = 0; idx < COUNTOF(form); idx++)
    	{
    		printf(TEXT("[%d] x:%d y:%d sData:%s chData:%s\n"), 
    			idx, form[idx].x, form[idx].y, form[idx].sData, form[idx].chData);
    	}
    	return 0;
    }
    Code:
    E:\Temp\679>cl 679.cpp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    679.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:679.exe
    679.obj
    Code:
    E:\Temp\679>679
    [0] x:30 y:40 sData:Name chData:N/A
    [1] x:30 y:42 sData:Address chData:N/A
    [2] x:30 y:44 sData:Address chData:N/A
    [3] x:30 y:50 sData:Address chData:N/A
    Last edited by Igor Vartanov; April 9th, 2013 at 05:49 AM.
    Best regards,
    Igor

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

    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.

  6. #21
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,928

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

    Quote Originally Posted by ADSOFT View Post
    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.
    Do you understand what the lines really instruct compiler to do?
    Code:
    { 30, 40, 1,data("test"), "N/A" },
    { 30, 42, 1,temp("TEST") , "N/A" },
    This data("test") must be either explicit calling constructor data::data(char*), or calling function int data(char*) defined somewhere in the scope. The same thing happens to temp("TEST"). This is definitely not what you intended to do.
    Best regards,
    Igor

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

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

    I wrote this function which worked in my Formview App(had to do it another way):


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

    char *format;
    };

    CString gSin = "gSin";
    CString CSZ("CityStZip");
    char str[120];
    CString temp = "Temp";



    struct print form [4];


    void set (int i, int x, int y, CString Data){

    form[i].x = x;
    form[i].y = y;
    form[i].data = Data;

    }


    void init_struct (void){
    set(0,30,40, CSZ);
    set(1,30,42, gSin);
    set(2,30,44, temp);
    set(3,30,46, CSZ);

    }
    Rate this post if it helped you.

  8. #23
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,928

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

    Quote Originally Posted by ADSOFT View Post
    (had to do it another way)
    See post #19
    Best regards,
    Igor

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

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

    Quote Originally Posted by Igor Vartanov View Post
    Sure it didn't. Why does your structure has two members named data? Why you initialization list entries differ in passed types?

    Tried the following and it worked just fine.

    Code:
    #include <windows.h>
    #include <atlstr.h>
    
    #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
    
    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString sData;
    
    char *chData;
    };
    
    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" },
    
    };
    
    
    int main() 
    {
    	for (int idx = 0; idx < COUNTOF(form); idx++)
    	{
    		printf(TEXT("[%d] x:%d y:%d sData:%s chData:%s\n"), 
    			idx, form[idx].x, form[idx].y, form[idx].sData, form[idx].chData);
    	}
    	return 0;
    }
    Code:
    E:\Temp\679>cl 679.cpp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    679.cpp
    Microsoft (R) Incremental Linker Version 10.00.40219.01
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    /out:679.exe
    679.obj
    Code:
    E:\Temp\679>679
    [0] x:30 y:40 sData:Name chData:N/A
    [1] x:30 y:42 sData:Address chData:N/A
    [2] x:30 y:44 sData:Address chData:N/A
    [3] x:30 y:50 sData:Address chData:N/A

    thanks for that example,

    I copied the area where you defined the vars and struct, still won't compile. Maybe it's the compiler these guys are using?
    V6.0, lol.

    I will have to ask them if they want to upgrade.

    I'm going to try your program in a CONSOLE app see what happens. If you have a V6.0 compiler handy try your program in it, I'm curious now.

    btw I understand about the temp("data"), thing, I was just trying as many CString constructor variations.
    Rate this post if it helped you.

  10. #25
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,296

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

    Quote Originally Posted by ADSOFT View Post
    I copied the area where you defined the vars and struct, . Maybe it's the compiler these guys are using?
    V6.0, lol.
    Again you do not provide enough information about what and how "still won't compile"! Why?

    Well, I can guess that the problem is in
    Code:
    #include <atlstr.h>
    Therre is no such a file in VC++6.0.
    In VC++6.0 you have to create a new project with MFC support and then copy/paste the code Igor created for you (and without any #includes!)
    Victor Nijegorodov

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

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

    Igor and Victor:

    I copied Igor's code to V6.0 compiler in a Console "Hello world app" which doesn't supply mfc support for CString so I added "#include <afx.h>" to the stdafx.h file.

    When I compiled Igor's program I got the following message:


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


    #include "stdafx.h"

    #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))

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

    char *chData;
    };

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

    };


    int main()
    {
    for (int idx = 0; idx < COUNTOF(form); idx++)
    {
    printf(TEXT("[%d] x:%d y:%d sData:%s chData:%s\n"),
    idx, form[idx].x, form[idx].y, form[idx].sData, form[idx].chData);
    }
    return 0;
    }


    Rate this post if it helped you.

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

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

    Victor and Igor

    Also created a console app w\MFC support and copied the initialization section of Igor's program:



    #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))

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

    char *chData;
    };

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

    };


    got the following error messages for each line of the struct member intialization code:

    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.

  13. #28
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,296

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

    As I wrote you in the post#2 your struct needs a ctor.
    This code compiles in VC++6:
    Code:
    #include "stdafx.h"
    
    #define COUNTOF(x)  (sizeof(x)/sizeof(x[0]))
    
    struct print
    {
    int x; //row
    int y; //col
    int fid;
    CString sData;
    
    char *chData;
    print(int xx, int yy, int id, const CString& data, char *ch) : 
    x(xx), y(yy), fid(id), sData(data), chData(ch) {}
    };
    
    CString temp("Address");
    
    print form[] =
    {
    	print( 30, 40, 1,CString("Name"), "N/A" ),
    	print( 30, 42, 1, temp , "N/A" ),
    	print( 30, 44, 1, temp, "N/A" ),
    	print( 30, 50, 1, temp, "N/A" ),
    
    };
    
    
    int main() 
    {
    	for (int idx = 0; idx < COUNTOF(form); idx++)
    	{
    		printf(TEXT("[%d] x:%d y:%d sData:%s chData:%s\n"), 
    			idx, form[idx].x, form[idx].y, form[idx].sData, form[idx].chData);
    	}
    	return 0;
    }
    Victor Nijegorodov

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

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

    Igor and Victor,

    It's starting to look like the compiler V6.0 , lol. Or maybe the constructor definitions of CString got updated.

    The compiler error message seems to imply that there might be "Ambigous intialization" when attempting to initialize a struct in the V6.0 definition. I couldn't see on when I looked at the constructor defs: http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx maybe you guys can see it?
    Rate this post if it helped you.

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

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

    Victor tried the ctor (copied your code directly) same compiler message ?:

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

    ?? maybe you updated your V6.0 sdk, on this computer it's not updated?


    strange?
    Rate this post if it helped you.

Page 2 of 3 FirstFirst 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center