enum doesn't seem to work correctly

W

Wiseguy

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?
 
A

Andrey Tarasevich

Wiseguy said:
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);

?
 
J

Jonathan Turkanis

Wiseguy said:
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top