Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > confusion defin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 43104 of 48020
Post > Topic >>

confusion defining a namespace

by Pep <pepaltavista@[EMAIL PROTECTED] > Feb 5, 2008 at 05:01 AM

Namespaces still confuse me when I try to get clever :(

Can I do the following

// file1.h
#ifndef __NS1_H__
#define __NS1_H__

namespace ns1
{
some base class
}
#endif // __NS1_H__

// file file1.cc
#include <file1.h>
implementation of ns1:: cl***** defined in file1.h

// file2.h
#ifndef __NS2_H__
#define __NS2_H__

#include <file1.h>

namespace ns1
{
some derived class
}
#endif // __NS2_H__

// file2.cc
#include <file2.h>
implementation of ns1:: cl***** defined in file2.h

then compile file1.cc and file2.cc in to a library called libns1.a

file1.h defines a base class that is used in a derived class defined
in file2.h , so I obviously need to include file1.h in to file2.h.

The reason I have both the base and derived cl***** in the same
namespace is that later in the project lifetime there will be other
cl***** derived from the base class in file1.h and the functionality
that the collection of derived cl***** provide is considered to be a
unique module within the business model, so from a development point
of view it would be ideal if all derived cl***** sit within the same
namespace. It does not feel right to have to define a new namespace
for each derived class.

I do not want to put the base class and all derived class definitions
in the same header file as that means that everything will need to be
recompiled each time a new derived class is defined. In our already
lengthy build process, adding more time by haveing to rebuild all
derived class implementation files just because we added a new one is
not desired.

I tried something like this and whilst the library libns1.a is built
okay (I say this because there are no errors in the build process) I
get problems when I try to use the library, as follows

//file main.cc
#include <file2.h>

int main(int argc, char** argv)
{
        // do something
}

When I compile main.cc and link against libns1.a, I get the following
error

libns1.a(file2.o) multiple definition of `namespaces' testns1.o:(.data
+0x0): first defined here

So where am I going wrong, is it that it is simply not possible to
extend the definition of a namespace across header files?

I am building this on a linux o/s with g++ 3.3.6

TIA,
Pete
 




 7 Posts in Topic:
confusion defining a namespace
Pep <pepaltavista@[EMA  2008-02-05 05:01:12 
Re: confusion defining a namespace
"Victor Bazarov"  2008-02-05 10:50:40 
Re: confusion defining a namespace
pelio <pelio@[EMAIL PR  2008-02-05 20:09:58 
Re: confusion defining a namespace
red floyd <no.spam@[EM  2008-02-05 19:35:17 
Re: confusion defining a namespace
Pep <pepaltavista@[EMA  2008-02-05 09:11:00 
Re: confusion defining a namespace
"Victor Bazarov"  2008-02-05 12:29:25 
Re: confusion defining a namespace
Pep <pepaltavista@[EMA  2008-02-05 10:09:57 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:45:00 CDT 2008.