I hate cstring!!!

Y

yeye yaya

Hello,
Why doesn't this work? it says

" error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called "

And is there a solution?

//Code starts here

void CDay2Dlg::OnRunpgm()
{
UpdateData(true);
CString strPgmName;
strPgmName = m_strProgToRun;
strPgmName.MakeUpper();

switch(strPgmName) {
case "PAINT":
WinExec("pbrush.exe",SW_SHOW);
break;
case "NOTEPAD":
WinExec("notepad.exe",SW_SHOW);
break;
case "SOLITAIRE":
WinExec ("sol.exe",SW_SHOW);
break;
}

}
 
S

Shane McDaniel

CString strPgmName;
strPgmName = m_strProgToRun;
strPgmName.MakeUpper();

switch(strPgmName) {


You can only switch on an int. Try using an if else statement

-shane
 
J

John Harrison

yeye yaya said:
Hello,
Why doesn't this work? it says

" error C2450: switch expression of type 'class CString' is illegal
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called "

Because switch expressions must be *integral*
And is there a solution?

No, at least not without a bit of work on your part, use your imagination.
And you're are blaming the wrong thing, CString isn't to blame.

john
 
M

Mike Smith

Ron said:
or you can put this all in a map
map<const char*, const char*> pgmmap;
pgmmap["PAINT"] = "pbrush.exe";
pgmmap["NOTEPAD"] = "notpad.exe";

WinExec(pgmmap[strPgmName], SW_SHOW);

Well, sure, if you don't mind calling WinExec() with NULL as a parameter
once in a while... ;-)

if (const char *p = pgmmap[strPgmName])
WinExec(p, SW_SHOW);
 
Y

yeye yaya

I am migrating from delphi, and with c++ I really got big problems to work
with strings. Any help would be welcome

thanx
 
S

Stuart Golodetz

Mike Smith said:
Ron said:
or you can put this all in a map
map<const char*, const char*> pgmmap;
pgmmap["PAINT"] = "pbrush.exe";
pgmmap["NOTEPAD"] = "notpad.exe";

WinExec(pgmmap[strPgmName], SW_SHOW);

Well, sure, if you don't mind calling WinExec() with NULL as a parameter
once in a while... ;-)

if (const char *p = pgmmap[strPgmName])
WinExec(p, SW_SHOW);

Or even (off the top of my head):

std::map<const char*,const char*>::iterator f = pgmmap.find(strPgmName);
if(f != pgmmap.end()) WinExec(*f, SW_SHOW);

Which avoids inserting another element into the map unnecessarily.

HTH,

Stuart.
 
S

Stuart Golodetz

yeye yaya said:
I am migrating from delphi, and with c++ I really got big problems to work
with strings. Any help would be welcome

thanx

Don't use CString if you can help it. Prefer std::string, since (a) it's
Standard (and hence portable) and (b) its design is better than that of
CString. The header you're looking for is <string>.

Example Usage:

std::string s = "blah";
s+=" and yet more " + s;
....
std::cout << s << '\n'; // outputs "blah and yet more blah"

HTH,

Stuart.

P.S. Take a look at "The C++ Standard Library" (Josuttis), it might prove
helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top