Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 15th, 2005, 1:20 AM   #1
kevin_cpp
Newbie
 
Join Date: May 2005
Posts: 1
Rep Power: 0 kevin_cpp is on a distinguished road
C++ namespace question

Hi all,

I'm trying to use the "namespace" feature of C++ but I'm getting linker errors (I'm using Visual Studios). It seems like the fix should be trivial but I don't know how to do it.

Here's what I'm doing:

-------- Main File --------------------
#include (the standard stuff)

#include "inner_file.h"
#include "outer_file.h"

int main()
{
inner::f();
...
outer::f();
}
-------- inner_file.h ------------
namespace inner
{
int A[10];
void f();
}
-------- inner_file.cpp ----------
#include "inner_file.h"
namespace inner
{
void f() { /* implementation */ }
}

Then I also have "outer_file.h" and "outer_file.cpp" but they're the same as the inner_files with the exception that the namespace is now "outer". When I compile it, I get a linker errorr saying that

inner::A already defined in main_file.obj
outer::A already defined in main_file.obj

(or something along those lines)

What am I doing wrong?

Thanks,

Kevin
kevin_cpp is offline   Reply With Quote
Old May 17th, 2005, 3:28 PM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
The problem is that you're defining variables in .h files, which you're not supposed to do.


int A[10];

don't do that in a .h file. The problem has nothing to do with namespaces, which you are using correctly.
uman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC