# namespace shinanigans

## Recommended Posts

namespace meo {
int random(int low, int high);
}


cpp
namespace meo {
int random(int low, int high) {
return ( ( rand() % (high - low + 1) ) + low );
}

}


then simply a call to meo::random(5,10); is generating unresolved externals. vc2008, recently just added a folder of my own to the include list, but that has been working fine, just this namespace bit is borking it up. [Edited by - BreathOfLife on September 14, 2008 8:44:41 PM]

##### Share on other sites
Missing comma between int low and int high in the first source code would generate a link error.

##### Share on other sites
unfortunately thats just in the post

the namespace was added to an existing header that worked just fine before, and I cant seem to get this namespace thingy working

then I just #include <random.h> and thats it. maybe im skipping something or leaving something out. do they still need to be manually added to the project?

##### Share on other sites
Quote:
 Original post by BreathOfLifeunfortunately thats just in the postthe namespace was added to an existing header that worked just fine before, and I cant seem to get this namespace thingy workingEDIT < more info > so this header/cpp file are in a folder that ive added to the paths and directories panal in the options menuthen I just #include and thats it. maybe im skipping something or leaving something out. do they still need to be manually added to the project?

If I'm understanding this correctly, you are wanting to use meo::random in multiple projects. I am inferring that you are building common utility code from all your projects.

The main problem is the headers are not providing the definitions to your functions. There are a couple of solutions... :

1) Include the definitions (random.cpp) in one of your project's .cpp to provide its definitions in random.h. This will generate the .cpp in question symbols and random.cpp symbols and combine them together into the .obj file used by the linker. Although... I'm not sure how this fairs in the "good programming practices" category.

2) Produce a static-link library and link your project to it. This will provide random.cpp's symbols in the library. However, it requires you to build the library and rebuild it each time you change it.

Hope this helps!

##### Share on other sites
The compiller will parse all .cpp in the solution browser, so you should had at least all the cpp files. The .h file get parsed when included from a .cpp file.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627674
• Total Posts
2978558

• 11
• 11
• 10
• 12
• 22