-
Grrr! So so easy.....
i have a header file named calg.h which i hoped would contain a global name space, but no matter what i try, i keep getting redefiniton errors, either at compile time, or at linker time.
Code:
//calg.h
#ifndef CALG_H
#define CALG_H
namespace alg
{
//this could easily change and could be used throughout the entire program
char possible_vars[] = "abcdefghijklmnopqrstuvwxyz"
}
#endif
main.cpp include complex.h which includes calg.h. main.cpp then include term.h which in turn include calg.h. i get the linker error that alg::possible_vars is already defined in complex.obj.
i know that defining the variable in the header is not good (to the extent of which, i don't know)..but i tried creating a calg.cpp and .............................
i just had a thought - i didn't make it const ............be right back .................. ok, i placed a const before the definition/declaration of possible_vars and it compiled (or linked rather) just fine.
what gives?
-
Probably because you have code / initialised data inside the header file.
This has always been a no-no, it's nothing to do with namespaces.
-
In the header, just declare the variable with the extern keyword.
Then initialize the variable in one source file.
-
hmmm.....
Code:
//aaa.h
extern "C" char a[];
namespace alg
{
char a[];
}
Code:
//aaa.cpp
#include "aaa.h"
char alg::a[] = "abcd";
seems like i should have to use: extern "C" char alg::a[];
nonetheless, thanks a lot Sang