I am trying to learn namespaces. in header exercise.h i wrote namespace
//excerpt from exercise.h
namespace trynamespace
{
class simpleclass
{
private:
int i;
public:
simpleclass(int init): i(init) {}
void set(int arg) {i=arg;}
void print(void) const {P(i);} //P is a #define
};
extern simpleclass simpleclassobj;
extern void blah(void);
}
in a cpp file i use the global object simpleclassobj and global function
void blah(void);
the definitions of the fns are in a *different* cpp file shown below
(Version1_Doesnt_Work)
However the code compiles fine but doesnt link.linker cant find
simpleclassobj and function blah. However when I use scope resolution
instead of using directive then everything works fine.!!.pls explain why
isnt the using directive fine? and why i had to use scope resolution to
make it work? thanks a lot.
//*Version1_Doesnt_Work* : implementation of global fn and definition of
//simpleclassobj
#include "exercise.h"
using namespace std;
using namespace trynamespace;
simpleclass simpleclassobj(11111);
void blah(void)
{
cout<<"fn blah called"<<endl;
}
------------------------------------------
// Version2_Works : implementation of global fn and definition of
//simpleclassobj
#include "exercise.h"
using namespace std;
//using namespace trynamespace; //*No need to use * using directive
trynamespace::simpleclass trynamespace::simpleclassobj(11111);
void trynamespace::blah(void)
{
cout<<"fn blah called"<<endl;
}