Doubt about namespaces

C

Carlos Martinez

//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
 
D

David Harmon

On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez
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
 
C

Carlos Martinez

David said:
On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez


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 .h

namespace ip {

class MyClass {
bool myMethod();
}

}
 
D

David Harmon

On Tue, 12 Sep 2006 10:13:43 +0200 in comp.lang.c++, Carlos Martinez


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();

}[/QUOTE]

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.
 
F

F.J.K.

Carlos said:
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.
 

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

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top