0

I'm currently using a namespace to hold a set of variables which should be usable by all classes in the project. Some variables should be constants and are defined in the namespace itself, for others the value should be assigned later. Example:

// MyProject.h
namespace MyNameSpace {
    extern const double a = 1.0;
    extern const int b = 2;
    extern double c;
    extern int d;
}

// MyProject.cpp
MyNameSpace::c = 3.0;
MyNameSpace::d = 4;

However this returns the errors for the variables c and d:

error LNK2001: unresolved external symbol "int MyNameSpace::c"
error LNK2001: unresolved external symbol "double MyNameSpace::d"

What is the correct way of assigning values to external variables outside of the namespace after declaration?

Marijn
  • 47
  • 1
  • 9

3 Answers3

1

extern means that variable will be defined somewhere else. so, you can't assign values to extern variable in your namespace. you need to define that variable in some cpp file float MyNameSpace::c = 3.0;. anyway, this doesn't seems like a way to go, you don't need externs here.

Sergi0
  • 1,084
  • 14
  • 28
  • If I remove the extern keyword and redefine it in my cpp as `double MyNameSpace::c = 3.0` it gives me the error `variable MyNameSpace::c cannot be defined in the current scope`. So your method doesnt seem to work for me... – Marijn May 13 '15 at 14:17
  • 1
    if you remove extern you don't need to specify the type anymore, just use MyNameSpace::c = 3.0 – Sergi0 May 13 '15 at 14:20
  • @Sergi0: If you remove the `extern`, then you'll break the One Definition Rule if the header is included from more than one translation unit. – Mike Seymour May 13 '15 at 14:30
  • @MikeSeymour well, cause this method is obviously wrong in the first place – Sergi0 May 13 '15 at 14:33
  • @Sergi0: It's not obviously wrong. Global variables are often unwise, but they sometimes have their uses. – Mike Seymour May 13 '15 at 14:34
  • @MikeSeymour I meant using externs here, cause it doesn't achieve the goal to define variables inside the namespace. – Sergi0 May 13 '15 at 14:37
  • @Marijn look here: http://stackoverflow.com/questions/3670031/static-string-constants-in-class-vs-namespace-for-constants-c – Sergi0 May 13 '15 at 14:38
  • The errors were related to other LNK2005 errors for the same variables. I explicitely tried to define the namespace only once, however it still got defined multiple times. The solutions in this thread solved the issues: http://stackoverflow.com/questions/10046485/error-lnk2005-already-defined – Marijn May 13 '15 at 14:45
  • @Marijn the problem with that solution is that you won't see the actual values by looking at the namespace. you will need to browse the cpp file. so I would prefer a solution where values are visible in the declaration. – Sergi0 May 13 '15 at 14:50
1

You don't want to assign them; you want to define them.

// MyProject.cpp
double MyNameSpace::c = 3.0;
int MyNameSpace::d = 4;

Depending on how they're used, you might also need to define a and b. Since they're const, you could remove extern from the declaration to define them as static variables; or you could leave the extern declarations alone and add definitions without initialisers to the cpp file.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

When the extern keyword is used, applied to a variable, it declares a variable but it does not define it. Simply put, there's no memory allocated so far for that variable; only a definition that the variable c is declared and is of type float. My best suggestion would be to define variables c and d somewhere in your code to default values (that is, if you really need extern in the architecture of your program).

Duarte Patrício
  • 133
  • 1
  • 10