Doubt about namespaces



 DEVELOP > c-Plus-Plus > Doubt about namespaces

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Carlos Martinez"
Date: 12 Sep 2006 03:13:43 AM
Object: Doubt about namespaces
//general.h
#ifndef _GENERAL_H
#define _GENERAL_H
namespace ip {
bool esServicioIP();
}
#endif //_GENERAL_H
//general.cc
#include <general.h>
....
using namespace ip;
bool esServicioIP() {
....
}
//distsgip.cc
.....
if(esServicioIP()) {
...
}
Code compiles ok, but linker gives me an error saying ip::esServicioIP
is undefined.
Instead if I put namespace ip when defining esServicioIP:
bool ip::esServicioIP() {
....
}
Code compiles and links ok.
In many other pieces of code with clases instead of functions inside a
namespace, I don't need to qualify class' methods with namespace:
bool MyClass::myMethod() {
....
}
Compiles and links ok
Is there any rule (perhaps of naming resolution or something else)
different for classes and normal functions?
Thanks in advance
.

User: "F.J.K."

Title: Re: Doubt about namespaces 12 Sep 2006 08:57:03 AM
Carlos Martinez wrote:

Is there any rule (perhaps of naming resolution or something else)
different for classes and normal functions?

Thanks in advance

No. Try harder to get a minimum testcase, as required by the FAQ and
you'll see.
.

User: "David Harmon"

Title: Re: Doubt about namespaces 12 Sep 2006 06:28:45 AM
On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
<cmg250@nospam.tid.es> wrote,

using namespace ip;

bool esServicioIP() {
...
}

That function is not "inside" the ip namespace. It is a new
definition of its own independent function.

In many other pieces of code with clases instead of functions inside a
namespace, I don't need to qualify class' methods with namespace:

bool MyClass::myMethod() {
...
}

That function must be a member of some namespace or class known as
MyClass that already exists. It's not a valid declaration of
MyClass. Furthermore, MyClass::myMethod must have already been
declared inside the namespace or class.

Is there any rule (perhaps of naming resolution or something else)
different for classes and normal functions?

Not really. For example,
using namespace ip;
MyClass foo; // has no connection with any ip::foo
class bar; // has no connection with any ip::bar
.
User: "Carlos Martinez"

Title: Re: Doubt about namespaces 12 Sep 2006 06:36:31 AM
David Harmon wrote:

On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
<cmg250@nospam.tid.es> wrote,

using namespace ip;

bool esServicioIP() {
...
}


That function is not "inside" the ip namespace. It is a new
definition of its own independent function.

Excuse me. I have ommited code in .h because I have considered obvious.
In .h
namespace ip {
bool esServicioIP();
}


In many other pieces of code with clases instead of functions inside a
namespace, I don't need to qualify class' methods with namespace:

bool MyClass::myMethod() {
...
}

In .h
namespace ip {
class MyClass {
bool myMethod();
}
}


That function must be a member of some namespace or class known as
MyClass that already exists. It's not a valid declaration of
MyClass. Furthermore, MyClass::myMethod must have already been
declared inside the namespace or class.

Is there any rule (perhaps of naming resolution or something else)
different for classes and normal functions?


Not really. For example,

using namespace ip;
MyClass foo; // has no connection with any ip::foo
class bar; // has no connection with any ip::bar

.
User: "David Harmon"

Title: Re: Doubt about namespaces 12 Sep 2006 07:22:30 AM
On Tue, 12 Sep 2006 13:36:31 +0200 in comp.lang.c++, Carlos Martinez
<cmg250@nospam.tid.es> wrote,

David Harmon wrote:

On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
<cmg250@nospam.tid.es> wrote,

using namespace ip;

bool esServicioIP() {
...
}


That function is not "inside" the ip namespace. It is a new
definition of its own independent function.


Excuse me. I have ommited code in .h because I have considered obvious.

In .h

namespace ip {

bool esServicioIP();

}

You are quite right, it is obvious enough. You have two functions.
In .h you declare ip::esServicioIP().
in .cc you define esServicioIP().
There is no reason you cannot have both of them. The compiler can
only hope you define ip::esServicioIP() somewhere too.
.




  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER