help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a valid statement??


From: Bernd Strieder
Subject: Re: a valid statement??
Date: Tue, 29 Apr 2008 15:30:14 +0200
User-agent: KNode/0.10.4

Hello,

jalqadir@gmail.com wrote:

> In my program I have the following statements
> 
> void Person::setTitle(const Glib::ustring& str) {
>     switch ( str ) {
>     case "None" : {
>         title = title_t::None;
>         break;
>     }
>     case "Dr"   : {
>         title = title_t::Dr;
>         break;
>     }
> ......
>     case "Rabbi" :{
>         title = title_t::Rabbi;
>         break;
>    }
>    case "Shaykh" :{
>         title = title_t::Shaykh;
>         break;
>    }
>     default     : {
>         title = title_t::None;
>         break;
>     }
>     }// switch
> }//Method
> 
> when compiling I get an error code that reads:
> ==== error: switch quantity not an integer  ====
> Which is true, but according to
> 
> http://newdata.box.sk/bx/c/htm/ch07.htm#Heading54

None of the examples there uses anything different from int. You'd
better use a textbook on the language to read on the restrictions of
switch, not some bogus information in a document focuses on some
library. The error is perfectly ok, so this is not a gcc problem.

You could use some map to map the strings to values to avoid the
if..else if...else if cascade.

Maybe like the following.

typedef std::map<Glib::ustring&, title_type> strToTitleType;
strToTitleType strToTitle;
...
...initStrToTitle() {
strToTitle["None"] = title_t::None;
...
strToTitle["Shayk"] = title_t::Shayk;
}
....

void Person::setTitle(const Glib::ustring& str) {
  strToTitleType::const_iterator sit = strToTitle.find(str);
  if (sit == strToTitle.end())
    title = title_t::None;
  else
    title = sit->second;
}

Bernd Strieder



reply via email to

[Prev in Thread] Current Thread [Next in Thread]