Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. classes and namespaces

classes and namespaces

Scheduled Pinned Locked Moved C / C++ / MFC
c++designquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Chambers
    wrote on last edited by
    #1

    Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is? class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; }; "When I left you I was but the learner, now I am the master" - Darth Vader

    B M _ 3 Replies Last reply
    0
    • A Alan Chambers

      Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is? class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; }; "When I left you I was but the learner, now I am the master" - Darth Vader

      B Offline
      B Offline
      Bob Ciora
      wrote on last edited by
      #2

      At what point do you get the compiler error? The line Type m_type; is invalid. You'd have to scope it with Outcome::Type m_type; Bob Ciora

      A 1 Reply Last reply
      0
      • B Bob Ciora

        At what point do you get the compiler error? The line Type m_type; is invalid. You'd have to scope it with Outcome::Type m_type; Bob Ciora

        A Offline
        A Offline
        Alan Chambers
        wrote on last edited by
        #3

        I changed it to this but I still get the same error message from gcc : "error: parse error before `namespace'" Do you think its a compiler bug or is it just not supposed to be supported? Its a bit weird don't you think considering that a class is essentially a namespace and you can have nested class declarations and nested namespace declarations, but not this way round? class Outcome { public: enum Type { kHalt, kMove, kJump, }; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; Outcome::Type m_type; }; "When I left you I was but the learner, now I am the master" - Darth Vader

        D 1 Reply Last reply
        0
        • A Alan Chambers

          Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is? class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; }; "When I left you I was but the learner, now I am the master" - Darth Vader

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          Namespaces can contain classes, but classes cannot contain namespaces. Stability. What an interesting concept. -- Chris Maunder

          A 1 Reply Last reply
          0
          • M Mike Dimmick

            Namespaces can contain classes, but classes cannot contain namespaces. Stability. What an interesting concept. -- Chris Maunder

            A Offline
            A Offline
            Alan Chambers
            wrote on last edited by
            #5

            I'm guessing its because namespace functions have global linkage within the namespace but by default functions in a class only have local linkage via an object? "When I left you I was but the learner, now I am the master" - Darth Vader

            T 1 Reply Last reply
            0
            • A Alan Chambers

              Is there any particular reason why the following code won't compile? Thats very basically what I have got. I'm only doing this as a quick hack, so I'm not at all concerned with code design, but it really intrigued me as to why it won't let me declare a namespace within a class? All the things I've read on the internet deal only with class declarations within a namespace but not visa versa. Just wondering whether this the c++ standard leaves it up to the compiler or whether its officially illegal code? Seems a bit silly to me, but maybe theres a reason for it? Just wondered if anyone's knows what it is? class Outcome { public: enum Type { kHalt, kMove, kJump, }; Type m_type; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; }; "When I left you I was but the learner, now I am the master" - Darth Vader

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              I can think of one reason. Many classes can belong to one namespace. So by giving 'using namespace' you have access to all the classes that are contained in that namespace. If what you mentioned is possible, then by giving, let's say, 'using namespace jump', you cannot create an object of class Outcome because all members of the class does not belong to that namespace. « Superman »

              1 Reply Last reply
              0
              • A Alan Chambers

                I changed it to this but I still get the same error message from gcc : "error: parse error before `namespace'" Do you think its a compiler bug or is it just not supposed to be supported? Its a bit weird don't you think considering that a class is essentially a namespace and you can have nested class declarations and nested namespace declarations, but not this way round? class Outcome { public: enum Type { kHalt, kMove, kJump, }; namespace halt { void Low(void); void Medium(void); void High(void); }; namespace move { void Low(void); void Medium(void); void High(void); }; namespace jump { void Low(void); void Medium(void); void High(void); }; Outcome::Type m_type; }; "When I left you I was but the learner, now I am the master" - Darth Vader

                D Offline
                D Offline
                digwizfox
                wrote on last edited by
                #7

                You do not have to qualify type with the name of the class since it is declared inside the class. The original declaration of the Type enumerated variable was fine. Shawn

                1 Reply Last reply
                0
                • A Alan Chambers

                  I'm guessing its because namespace functions have global linkage within the namespace but by default functions in a class only have local linkage via an object? "When I left you I was but the learner, now I am the master" - Darth Vader

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  Alan Chambers wrote: functions in a class only have local linkage via an object? member functions of a class receive the this pointer, which allow the functions to point directly the object that is using the function. a particular point is the static member functions -> no this pointer as implicit parameter


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups