0

In a header I have a setup like this

namespace NS {
    typedef enum { GOOD, BAD, UGLY }enum_thing;
    class Thing {
        void thing(enum_thing elem);
    }
}

and of course another cpp file that goes along with that header. Then I have a thread cpp file that contains main(). In this cpp file I use that enum to pass to the method thing().

using namespace NS;
int main() {
    Thing t();
    t.thing(BAD);
}

and of course I get other errors from G++ saying BAD was not declared. Any help on how I could overcome this error?

3
  • 1
    it should be Thing t; not Thing t() Commented Jul 7, 2010 at 22:36
  • a public: is missing before the method and a ; is missing at the end of the class declaration, after the closing }. Commented Jul 7, 2010 at 22:41
  • What if you fully qualify the enum: NS::BAD. Older compiler dislike this. typedefing in C++ is not needed in such cases. Here an anonymous enum is getting typedef'ed. Commented Jul 7, 2010 at 22:44

3 Answers 3

5

After correcting numerous little syntax errors in the sample code, it compiles just fine for me. Check that you've spelled the names correctly. Can you access the enum as NS::BAD? Perhaps you haven't included the correct header? Make sure you have #include "FileWithEnum.h" at the top.

namespace NS {
    typedef enum { GOOD, BAD, UGLY }enum_thing;
    class Thing {
        public:
            void thing(enum_thing elem){}
    };
}


using namespace NS;
int main() {
    Thing t;
    t.thing(BAD);
    return 0;
}

Test it yourself: http://codepad.org/Uw0XjOlF

Sign up to request clarification or add additional context in comments.

Comments

2

Can you avoid using a typedef? Just do:

enum Foobar {good, bad, hello};

Comments

0

It should work. It does for me (the variant by Mystagogue should also work). I understand you had some other error messages ?

You probably just have to fix the header to be syntaxically correct, like putting a semi-colon at the end of class Thing, etc. When the header will be OK, the message about BAD not in namespace should also disappear.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.