Reputation: 701
Folks,
I crated a name space in a single file, but how exactly do I separate the definition and declaration? Can I create a .h for declaration and a .cpp for definition just like in a class set of file? If yes, then, do I need to include the namespace header file in my new program, or is the using namspace mynamespace enough?
Upvotes: 2
Views: 8146
Reputation: 597941
Any decent C++ book should cover namespaces and code separation.
Try something like this:
myfile.h
#ifndef myfile_H
#define myfile_H
namespace myns
{
class myclass
{
public:
void doSomething();
};
}
#endif
myfile.cpp:
#include "myfile.h"
void myns::myclass::doSomething()
{
//...
}
And then you can use it like this:
#include "myfile.h"
myns::myclass c;
c.doSomething();
Or:
#include "myfile.h"
using namespace myns;
myclass c;
c.doSomething();
Upvotes: 12