namespace shinanigans

Started by
3 comments, last by Dunge 15 years, 7 months ago
header
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]
Advertisement
Missing comma between int low and int high in the first source code would generate a link error.
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

EDIT < more info > so this header/cpp file are in a folder that ive added to the paths and directories panal in the options menu

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?
Quote:Original post by BreathOfLife
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

EDIT < more info > so this header/cpp file are in a folder that ive added to the paths and directories panal in the options menu

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?



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

This topic is closed to new replies.

Advertisement