namspace qn

T

trying_to_learn

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;
}
 
J

John Harrison

trying_to_learn said:
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;
}

You have to understand the difference between looking up a name in a
namespace, and defining a name in a namespace.

'using namespace trynamespace' only affects the way names are looked up,
trynamespace gets added to the list of namespaces that will be searched for
a name. There are two ways to define a name in a namespace, one is

void trynamespace::blah()
{
}

the other is

namespace trynamespace
{
void blah()
{
}
}

john
 
A

angelo

trying_to_learn said:
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;
}

Which compiler are you using and what's its version? I think version 1
is correct and tried with g++3.3.1, then everything is fine.
You included "excersise.h" into the "exercise.cpp", so it's equal to
copy and paste the definition of namaspace trynamespace at the beginning
part of the .cpp file. and using namespace trynamespace will actually
make everything in trynamespace visible in the global namespace. So
actually your code equals to :
------------------------------------------------
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);

using namespace std;
simpleclass simpleclassobj(11111);

void blah(void)
{
cout<<"fn blah called"<<endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top