-
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?
-
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*?
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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
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.
-
Re: Can't intialize C++ Struct with CString in it??
D_Drmmr ,
tried the following it didn't work either?
Quote:
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" },
};
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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++!
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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.
-
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.
-
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?
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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.
-
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.
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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
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? :confused:
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?
-
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.
-
Re: Can't intialize C++ Struct with CString in it??
Here is some more info:
I'm getting the following message from the compiler:
Quote:
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.
Quote:
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" },
};
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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!
-
Re: Can't intialize C++ Struct with CString in it??
Changed the code to the following
Quote:
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:
Quote:
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
-
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" },
};
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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;
}
-
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
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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
-
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
Quote:
error C2440: 'initializing' : cannot convert from 'const int' to 'struct print'
No constructor could take the source type, or constructor overload resolution was ambiguous
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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.
-
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):
Quote:
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);
}
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
(had to do it another way)
See post #19
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
Igor Vartanov
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.
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
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
Quote:
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!)
-
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:
Quote:
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:
Quote:
#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;
}
-
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:
Quote:
#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:
Quote:
error C2440: 'initializing' : cannot convert from 'const int' to 'struct print'
No constructor could take the source type, or constructor overload resolution was ambiguous
-
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;
}
-
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?
-
Re: Can't intialize C++ Struct with CString in it??
Victor tried the ctor (copied your code directly) same compiler message ?:
Quote:
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?
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
Igor and Victor,
It's starting to look like the compiler V6.0 , lol.
First and foremost, VC 6.0 is not an ANSI C++ compiler. The answers that were given to you earlier assumed you were using an ANSI C++ compiler, such as later versions of Visual C++. In VC 6.0, perfectly valid ANSI C++ code will fail to compile, and also, invalid ANSI C++ code will compile on that old compiler.
Next time, you need to mention the exact compiler you're using.
Regards,
Paul McKenzie
-
Re: Can't intialize C++ Struct with CString in it??
Paul,
So why did it compile on Victor's V6.0 ?
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
Victor tried the ctor (copied your code directly) same compiler message ?:
?? maybe you updated your V6.0 sdk, on this computer it's not updated?
strange?
The bottom line is that VC 6.0 is not an ANSI C++ compiler. It has nothing to do with the SDK.
The C++ language was standardized in 1998. This is after VC 6.0 came out. So you have a computer language, C++, that was finally standardized after the compiler you're using was created. Do you see a problem with that? The obvious problem is that the C++ language contains rules that never made it into the VC 6.0, and one of those rules is construction inside an initialization.
Regards,
Paul McKenzie
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
Paul,
So why did it compile on Victor's V6.0 ?
Service packs.
My main point is that you were getting upset when the ones helping you had no idea you were using an outdated compiler.
Regards,
Paul McKenzie
-
Re: Can't intialize C++ Struct with CString in it??
Quote:
Originally Posted by
ADSOFT
Victor tried the ctor (copied your code directly) same compiler message ?:
?? maybe you updated your V6.0 sdk, on this computer it's not updated?
strange?
Well, my VC++6.0 has SP6 installed.
Besides there is also PSDK from February 2003.
So I cannot test on the "clean" VC++6.0
-
Re: Can't intialize C++ Struct with CString in it??
Well, I'm glad we got to the bottom of it. I knew there was a logical explination.
It explains why just about every contruction I tried of the CString didn't work, lol.
Thanks to everyone for all the help.
Sorry to see VS6.0 take one in the shorts, but then again for pre ANSI it did a gr8 job.
-
Re: Can't intialize C++ Struct with CString in it??
Victor I agree.
Thanks for help, what can you do, Pre-Ansi, that's a tough one to deal with, but it a wierd way it was fun figuring it out.
Thanks again.