-
switch(??)
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
[url]http://newdata.box.sk/bx/c/htm/ch07.htm#Heading54[/url]
the error is unwarranted, since the parameter received by the method
and later on given to the switch statement for scrutiny is a valid C/C+
+ statement [setTitle(const Glib::ustring& str)]
Can anyone shed some light to this problem?
-
Re: switch(??)
>[color=blue]
> Can anyone shed some light to this problem?[/color]
you would have better luck by asking in a c++ newsgroup.
the switch statement can only process numerical values like integers
(or chars), pointers etc.
Passing a c++ construct, like a std::string, does not work since no
cast to a number is possible, hence the error dumped by the compiler.
Using switch to have a dispatch based on strings (without hashing
them) is not feasible.
One way to do such thing would be something like:
#include <utility>
// provide the mapping between input strings and their associated
// results taken from the switch
std::pair< std::string, std::string > mapping[] =
{
std::make_pair( "None", title_t::None ),
...
};
or use a map, sorted vector of pairs with binary search and set your
title accordingly:
- build map, array of pairs, vector etc.
- lookup for the string you are looking for
- fetch the corresponding mapping
And grab a good about programming in C++ like "accelerated C++" by
Koenig and Moo or "C++ primer" - no offense here - you'd better know
you are dealing with a very efficient language that use a lot of
different paradigms/constructs (BTW, grab anything you find that is
written by Koenig: he writes really good stuff).
-- paulo
-
Re: switch(??)
In <900c0f30-ed01-4e3b-b4db-f70b262a2d9d@f63g2000hsf.googlegroups.com> jamiil <jalqadir@gmail.com> writes:
[color=blue]
> void Person::setTitle(const Glib::ustring& str) {
> switch ( str ) {
> case "None" : {[/color]
You can't use switch() on strings. You'll have to use a series of
strcmp() calls (or whatever similar function Glib::ustring provides.)
--
John Gordon A is for Amy, who fell down the stairs
[email]gordon@panix.com[/email] B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
-
Re: switch(??)
On Wed, 30 Apr 2008 15:36:04 +0000 (UTC) John Gordon <gordon@panix.com> wrote:
| In <900c0f30-ed01-4e3b-b4db-f70b262a2d9d@f63g2000hsf.googlegroups.com> jamiil <jalqadir@gmail.com> writes:
|
|> void Person::setTitle(const Glib::ustring& str) {
|> switch ( str ) {
|> case "None" : {
|
| You can't use switch() on strings. You'll have to use a series of
| strcmp() calls (or whatever similar function Glib::ustring provides.)
Well, at least not in C. Maybe some higher level language may have implemented
such a syntax/semantic.
--
|WARNING: Due to extreme spam, I no longer see any articles originating from |
| Google Groups. If you want your postings to be seen by more readers |
| you will need to find a different place to post on Usenet. |
| Phil Howard KA9WGN (email for humans: first name in lower case at ipal.net) |
-
Re: switch(??)
Thanks folks, the problem has been resolved, thanks everyone.