enum doesn't seem to work correctly

Discussion in 'C++' started by Wiseguy, Jan 18, 2004.

  1. Wiseguy

    Wiseguy Guest

    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?
     
    Wiseguy, Jan 18, 2004
    #1
    1. Advertising

  2. 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
     
    Andrey Tarasevich, Jan 18, 2004
    #2
    1. Advertising

  3. "Wiseguy" <> wrote in message
    news:p...
    > 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
     
    Jonathan Turkanis, Jan 18, 2004
    #3
    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. Bill Green

    &nbsp; in a ListItem doesn't seem to work

    Bill Green, Feb 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    441
    vMike
    Feb 7, 2004
  2. Garrett
    Replies:
    4
    Views:
    4,854
    Garrett
    Dec 2, 2004
  3. tshad
    Replies:
    3
    Views:
    534
    MWells
    Jan 26, 2005
  4. Jeff Rodriguez
    Replies:
    5
    Views:
    407
    CBFalconer
    Nov 15, 2003
  5. Xeno Campanoli
    Replies:
    13
    Views:
    322
    Xeno Campanoli
    Jan 6, 2008
Loading...

Share This Page