Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Quick Namespace Question

namespace

  • Please log in to reply
5 replies to this topic

#1 RoboticForest

RoboticForest

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 130 posts
  • Location:Salem, OR
  • Programming Language:Java, C++

Posted 18 October 2010 - 09:42 PM

When making namespaces, do you have to declare them in the .cpp file, the .h file, or both? Is one merely redundant?

- Dave
  • 0

No trees were harmed in the sending of this message, but millions of electrons were severely inconvenienced.


#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 18 October 2010 - 11:17 PM

Well, a namespace would be useful in a library header (.h) for example, where the library may use "class String". If your code has "class String" then there will be conflicting code, so wrapping a namespace around the class library will be a safe practise. i.e.
namespace foo {
    class String() {
        public function outputSomething(){};
    }
}
#include "class.h"
int main() {
    foo::String s1, s2, s3;
    s1->outputSomething();
}

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 19 October 2010 - 12:48 AM

Note that when you are implementing a code for a class or whatever in *.cpp files, you must use one of these:
include "myfile.h"
using namespace foo;

MyClass::MyClass() { }

// or
#include "myfile.h"
namespace foo {
    MyClass::MyClass() { }
}

// or
#include "myfile.h"
foo::MyClass::MyClass() { }
void foo::MyClass::someFunction() { }

  • -1

The roots of education are bitter, but the fruit is sweet.


#4 RoboticForest

RoboticForest

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 130 posts
  • Location:Salem, OR
  • Programming Language:Java, C++

Posted 19 October 2010 - 08:12 PM

Okay. So it would be in both then?

Ball.h:

namespace Library {
    class Ball{
        void bounce(int distance);
    };
}
Then, in Ball.cpp:

namespace Library{
    void Ball::bounce(int distance){
        // Code here.
    }
}

  • 0

No trees were harmed in the sending of this message, but millions of electrons were severely inconvenienced.


#5 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 19 October 2010 - 08:24 PM

It could be in both files if you do not wish to type "Library::" in "Library::Ball::bounce" each time you reference the namespace. All of FlyingDutchman's examples are valid choices though.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#6 RoboticForest

RoboticForest

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 130 posts
  • Location:Salem, OR
  • Programming Language:Java, C++

Posted 19 October 2010 - 08:37 PM

Perfect. Thank you both. :)
  • 0

No trees were harmed in the sending of this message, but millions of electrons were severely inconvenienced.






Also tagged with one or more of these keywords: namespace

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download