namespaces question

Get answers to all your basic programming questions. No Ogre questions, please!
sergey akifiev
Kobold
Posts: 34
Joined: Mon Feb 09, 2004 7:56 am

namespaces question

Post by sergey akifiev »

hi. i've got dumb question about namespaces. currently I'm learning C++ / refreshing C skills with "Thinking in C++" by Bruce Eckel. In chapter 10 there is a task:
# Repair the problem in OverridingAmbiguity.cpp, first with scope resolution, then instead with a using declaration that forces the compiler to choose one of the identical function names.

all right. here is the code:
NamespaceMath.h :

Code: Select all

#ifndef NAMESPACEMATH_H
#define NAMESPACEMATH_H
#include "NamespaceInt.h"
namespace Math {
  using namespace Int;
  Integer a, b;
  Integer divide(Integer, Integer);
  // ...
} 
#endif // NAMESPACEMATH_H ///:~
NamespaceOverriding2.h :

Code: Select all

#ifndef NAMESPACEOVERRIDING2_H
#define NAMESPACEOVERRIDING2_H
#include "NamespaceInt.h"
namespace Calculation {
  using namespace Int;
  Integer divide(Integer, Integer);
  // ...
} 
#endif // NAMESPACEOVERRIDING2_H ///:~
and finally OverridingAmbiguity.cpp :

Code: Select all

#include "NamespaceMath.h"
#include "NamespaceOverriding2.h"
void s() {
  using namespace Math;
  using namespace Calculation;
  // Everything's ok until:
  Math::divide(1, 2); // Ambiguity
}
int main() {}
when compiling, i have next errors:

Code: Select all

cd /home/sergey/work/c++/tasks/c10/
make -k OverridingAmbiguity
g++ -oOverridingAmbiguity OverridingAmbiguity.o
OverridingAmbiguity.o(.text+0x50): In function `s()':
: undefined reference to `Math::divide(Int::Integer, Int::Integer)'
collect2: ld returned 1 exit status
make: *** [OverridingAmbiguity]  1

Compilation exited abnormally with code 2 at Sun Jan 16 12:48:45
WHY??? i have Math::divide() defined and declared in header!!!
pls help, or my braing just blows away.

PS sorry for bad english :-)
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 538

Post by Kojack »

Has Math::divide actually been implemented in a cpp file somewhere? And it exactly matched the prototype in the header? (not a capital D)

The linking error means it's compiled ok, it knows which divide to call, it just can't find the actual implementation of the divide.
sergey akifiev
Kobold
Posts: 34
Joined: Mon Feb 09, 2004 7:56 am

Post by sergey akifiev »

OMG! how stupid and unattentive i was.
thanks a lot. adding `{}` in *.h in divide()'s definition resolves trouble.
User avatar
Emmeran
Goblin
Posts: 272
Joined: Wed Jun 02, 2004 11:47 am
Location: Erlangen

Post by Emmeran »

btw why don't u jsut use classes???
sergey akifiev
Kobold
Posts: 34
Joined: Mon Feb 09, 2004 7:56 am

Post by sergey akifiev »

what do you mean? i'm just doing exercises.
User avatar
Emmeran
Goblin
Posts: 272
Joined: Wed Jun 02, 2004 11:47 am
Location: Erlangen

Post by Emmeran »

i meant this:

Code: Select all

class Int
{
    static float divide(int a, int b) { return a / b; }
};

printf("10 divided by 3 is: %f\n", Int::divide(10, 3));
User avatar
haffax
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4823
Joined: Fri Jun 18, 2004 1:40 pm
Location: Berlin, Germany
x 8

Post by haffax »

Emmeran this code is only of academic use, to learn about namespaces. No need to improve it. ;)
team-pantheon programmer
creators of Rastullahs Lockenpracht
User avatar
Kencho
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 4011
Joined: Fri Sep 19, 2003 6:28 pm
Location: Burgos, Spain
x 2

Post by Kencho »

Anyways, using namespaces is better for large applications, as you define independence between large modules. Namespaces are the same as Packages in Java and UML.
Image