I hate cstring!!!

Discussion in 'C++' started by yeye yaya, Jul 11, 2003.

  1. yeye yaya

    yeye yaya Guest

    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;
    }

    }
     
    yeye yaya, Jul 11, 2003
    #1
    1. Advertising


  2. > CString strPgmName;
    > strPgmName = m_strProgToRun;
    > strPgmName.MakeUpper();
    >
    > switch(strPgmName) {
     
    Shane McDaniel, Jul 11, 2003
    #2
    1. Advertising


  3. > CString strPgmName;
    > strPgmName = m_strProgToRun;
    > strPgmName.MakeUpper();
    >
    > switch(strPgmName) {



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

    -shane
     
    Shane McDaniel, Jul 11, 2003
    #3
  4. "yeye yaya" <> wrote in message
    news:ben107$9rp$...
    > 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
     
    John Harrison, Jul 11, 2003
    #4
  5. yeye yaya

    Mike Smith Guest

    Ron Natalie wrote:
    >
    > 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);

    --
    Mike Smith
     
    Mike Smith, Jul 11, 2003
    #5
  6. yeye yaya

    yeye yaya Guest

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

    thanx
     
    yeye yaya, Jul 11, 2003
    #6
  7. Mike Smith <> wrote in message
    news:...
    > Ron Natalie wrote:
    > >
    > > 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.

    > --
    > Mike Smith
     
    Stuart Golodetz, Jul 11, 2003
    #7
  8. yeye yaya <> wrote in message
    news:ben5pn$oct$...
    > 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.
     
    Stuart Golodetz, Jul 11, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    4,618
  2. Rajesh Garg

    Why is CString not preferred

    Rajesh Garg, Jul 22, 2003, in forum: C++
    Replies:
    11
    Views:
    816
    Mike Wahler
    Jul 24, 2003
  3. Markus Hämmerli
    Replies:
    6
    Views:
    7,896
    Peter van Merkerk
    Sep 4, 2003
  4. John Smith

    Parsing char array to CString

    John Smith, Sep 6, 2003, in forum: C++
    Replies:
    4
    Views:
    7,265
    Pat McCormack
    Sep 9, 2003
  5. lallous

    CString question

    lallous, Jan 7, 2004, in forum: C++
    Replies:
    3
    Views:
    10,606
    Chris Theis
    Jan 7, 2004
Loading...

Share This Page