Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:00:15+00:00 2026-06-14T01:00:15+00:00

The problem is C++ does not allow namespace declarations within classes.(I searched on internet

  • 0

The problem is C++ does not allow namespace declarations within classes.(I searched on internet and found this; if it’s not true, please say it) So, what’s the best way to cheat this problem?

Context: My class have an enumeration within it.

class GameCharacter {
public:
    enum MovingState {
        Running,
        Walking,
        Stopped
    };
    ...
};

OBS: This example is not real, it’s totally hypothetical.

C++ defines that the enumeration names are inside the class scope, then to use these states I have to use the scope operator directly from the class name(GameCharacter::Running, for instance; or using GameCharacter::Running).

I think this is bad, because the name which belongs to the enumeration is inside the class scope; I wanted to have a scope for the RunningState enumeration. (accessing it this way: GameCharacter::MovingState::Running)
My first thought, then, was to create a namespace which would define a scope for the enumeration.

class GameCharacter {
public:
    // does not compile
    namespace MovingState {
        enum State {
            Running,
            Walking,
            Stopped
        };
    };
    ...
};

But C++ forbids it. This code does not compile. (main.cpp:3:5: error: expected unqualified-id before ‘namespace’)

The reason why I’m trying to do things this way is because there is a possibility to create an second enumeration with names in same scope. Which could cause conflicting names.

class GameCharacter {
public:
    enum MovingState {
        Running,
        Walking,
        Stopped
    };
    enum DrivingState {
        Accelerating,
        Breaking,
        Stopped        // Compilation error: conflicts with previous declaration ‘GameCharacter::MovingState GameCharacter::Stopped’
    };
    ...
};

(my idea was that, in this case, the states should be referred as GameCharacter::MovingState::Stopped and GameCharacter::DrivingState::Stopped)
Then, what should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:00:16+00:00Added an answer on June 14, 2026 at 1:00 am

    In C++1x (the new version of C++ that was standardised last year), you can use strongly typed enumerations, which put the identifiers in the scope of the enum class itself (among other improvements). These are declared with enum class:

    class GameCharacter {
    public:
        enum class State {
            Running,
            Walking,
            Stopped    // Referred to as GameCharacter::MovingState::Stopped.
        };
        ...
    };
    

    In the meantime, if you’re stuck with a C++03 compiler or want to retain compatibility, just use class instead of namespace and declare the constructor private as you suggested in your own answer. You can make the entire class private if the outside world doesn’t need to see it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: does not allow me to renderBinary . The method renderBinary(InputStream) in the type
The root of the problem for me is that Java does not allow references.
I am having the same problem as this post but the answer does not
I have a problem html macro.As I realized html macro does not allow insert
Is it true that Mathematica's Minimize function does not allow constraints like Mod[x,2]==0? I
Tomcat does not encode correctly String literals that contain unicode characters. The problem occurs
We got a problem with NUnit 2.5.3: nunit-console.exe does not return after finishing all
I have some problem. Dialog.dismiss() does not work. I want to input ip, username,
I'm having a problem dropping a temporary table. The user account does not have
I have a problem with Jquery function getJSON, the action url does not trigger

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.