Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > enum doesn't seem to work correctly

Reply
Thread Tools

enum doesn't seem to work correctly

 
 
Wiseguy
Guest
Posts: n/a
 
      01-18-2004
I have an enum defined within a namespace. When I reference the enum
using its qualified name graphics::db_status(VALUE) it does not work.

If I say "using namespace graphics;" then I can resolve the name of the
enumeration and its values.

The following code demonstrates the problem:


#include <db_RO.hpp>
/*
namespace graphics { enum db_status { FROM_FILE, FROM_DB, FROM_NOWHERE }; };
// actually defined in db_RO.hpp
*/
int main(int argc,char **argv) {
char ofile[255];
strcpy(ofile,"organ.cdbf");
if (argc>1) strcpy(ofile,argv[1]);

// scenario 1: DOES NOT WORK
graphics::db_RO thedb;
// next line generates "error: `FROM_DB' undeclared"
if (thedb.load(ofile)==graphics::db_status(FROM_DB))
thedb.save(ofile);

// scenario 2: DOES WORK
using namespace graphics;
db_RO thedb;
// however next line works in this case, why???
if (thedb.load(ofile)==db_status(FROM_DB))
thedb.save(ofile);

return 0;
};


Only scenario 2 works. In if() of the first scenario it makes no
difference if I say graphics::db_status(FROM_DB) or just
db_status(FROM_DB). Both generate the listed error condition.

Why???

I'm using gcc 3.3. Is this a compiler specific bug or am I using it
improperly?



 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      01-18-2004
Wiseguy wrote:
> I have an enum defined within a namespace. When I reference the enum
> using its qualified name graphics::db_status(VALUE) it does not work.
>
> If I say "using namespace graphics;" then I can resolve the name of the
> enumeration and its values.
>
> The following code demonstrates the problem:
>
>
> #include <db_RO.hpp>
> /*
> namespace graphics { enum db_status { FROM_FILE, FROM_DB, FROM_NOWHERE }; };
> // actually defined in db_RO.hpp
> */
> int main(int argc,char **argv) {
> char ofile[255];
> strcpy(ofile,"organ.cdbf");
> if (argc>1) strcpy(ofile,argv[1]);
>
> // scenario 1: DOES NOT WORK
> graphics::db_RO thedb;
> // next line generates "error: `FROM_DB' undeclared"
> if (thedb.load(ofile)==graphics::db_status(FROM_DB))
> thedb.save(ofile);


Both 'db_status' and 'FROM_DB' are identifiers from namespace
'graphics'. You have to specify the scope resolution operator for both

if (thedb.load(ofile) == graphics::db_status(graphics::FROM_DB))
thedb.save(ofile);

However, I don't understand what is the purpose of the redundant
typecast in the above code. Why don't you just write

if (thedb.load(ofile) == graphics::FROM_DB)
thedb.save(ofile);

?

> // scenario 2: DOES WORK
> using namespace graphics;
> db_RO thedb;
> // however next line works in this case, why???
> if (thedb.load(ofile)==db_status(FROM_DB))
> thedb.save(ofile);


This should work, but then again why not just

if (thedb.load(ofile) == FROM_DB)
thedb.save(ofile);

?

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      01-18-2004

"Wiseguy" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> I have an enum defined within a namespace. When I reference the

enum
> using its qualified name graphics::db_status(VALUE) it does not

work.
>
> If I say "using namespace graphics;" then I can resolve the name of

the
> enumeration and its values.
>
> The following code demonstrates the problem:
>
> // next line generates "error: `FROM_DB' undeclared"
> if (thedb.load(ofile)==graphics::db_status(FROM_DB))


Try:

if (thedb.load(ofile)==graphics::FROM_DB)

Jonathan





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DBI doesn't seem to install correctly on CentOS Xeno Campanoli Ruby 13 01-06-2008 09:08 PM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
get a line from file pointer & pointer doesn't seem to free correctly Jeff Rodriguez C Programming 5 11-15-2003 11:05 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments