473,505 Members | 15,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

namspace qn

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;
}
Jul 22 '05 #1
2 1252

"trying_to_learn" <no****@no.no> wrote in message
news:cm**********@gist.usc.edu...
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
Jul 22 '05 #2
trying_to_learn wrote:
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;
}
--------------------------------------------------
I see no problem with that.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1684
by: Tjerk Wolterink | last post by:
Hello all, I've written an XML file that looks a bit like this: <a> <space:b> content 1 </space:b> <b> content 2
1
1644
by: Lee Chapman | last post by:
Has anyone successfully been able to use an XmlValidatingReader (Framework 1.1) to validate documents against XML Schemas that haven't been added to the XmlValidatingReader.Schemas collection...
1
3204
by: Jeff Molby | last post by:
Sorry for the crossposting guys. I figured this one might be a little tricky, so I'm calling upon the C# brains out there to help out this poor VB developer. I know enough C# to translate any code...
2
1244
by: Showjumper | last post by:
I get this error after i changed the root namespace in the properties window for my vbnet server control since i didnt like the fact that vbnet uses the project name as the root namespace. Or do i...
6
1080
by: zs | last post by:
Hi! I have a question about converting from native c++ to c++/cli. How to convert/rewrite c++ code that looks like this to cli: //MyC.h namespace MyNS { MyClass
1
1344
by: luthriaajay | last post by:
I am using my own namespace called xmlns:i="http://www.ABC/int:i" in my XSL style sheet. The input XML document (which has its own namespace) needs to be mapped with the elements defined in my...
1
1200
by: pal jain8 | last post by:
hello, any body can be tell me how we use user crate name space in c# form. here i create name space in c# (2005) page namespace SampleNamespace { class SampleClass { public...
2
1626
by: Microsoft Newsserver | last post by:
Hi I have a web applications ( one with the dll ). and I have declared some classes. However, I keep getting warnings that the class c:\xxxx\xxxx\xxx.cs conflicts with the imported on the in...
2
2341
by: Hunter | last post by:
We are reviewing a vendor who will output some data in an XML format. I will then use python to convert the data to another format for upload to another vendor. I'm having trouble with very basic...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.