-
April 7th, 2013, 01:10 AM
#1
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.
-
April 7th, 2013, 02:30 AM
#2
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
-
April 7th, 2013, 06:10 AM
#3
Re: Can't intialize C++ Struct with CString in it??
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.
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.
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
-
April 8th, 2013, 04:55 PM
#4
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.
-
April 8th, 2013, 05:14 PM
#5
Re: Can't intialize C++ Struct with CString in it??
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++!
Victor Nijegorodov
-
April 8th, 2013, 07:29 PM
#6
Re: Can't intialize C++ Struct with CString in it??
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.
-
April 9th, 2013, 12:38 AM
#7
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.
-
April 9th, 2013, 01:00 AM
#8
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.
-
April 9th, 2013, 01:42 AM
#9
Re: Can't intialize C++ Struct with CString in it??
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.
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
-
April 9th, 2013, 02:18 AM
#10
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.
-
April 9th, 2013, 02:52 AM
#11
Re: Can't intialize C++ Struct with CString in it??
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:
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?
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
-
April 9th, 2013, 03:01 AM
#12
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.
-
April 9th, 2013, 03:16 AM
#13
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.
-
April 9th, 2013, 03:20 AM
#14
Re: Can't intialize C++ Struct with CString in it??
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!
Victor Nijegorodov
-
April 9th, 2013, 03:32 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|