I have two C++ namespaces as follows:
#ifndef TRANS_H
#define TRANS_H
namespace Trans
{
double Delta[3];
double calcDeltaPositions();
//and more that I will leave out for simplicity
};
#endif
#ifndef SPACE_H
#define SPACE_H
namespace Space
{
double vels[3];
void calcAccel(double DeltaVal[3]);
};
#endif
Now I have a main.cpp file:
#include "Trans.h"
#include "Space.h"
int main()
{
double pos = Trans::calcDeltaPositions();
Space::calcAccel(Trans::Delta);
return 0;
}
I keep getting an error claiming that Delta is a multiply defined in main.o and Trans.o How could this be since I have only declared Delta to exist in Trans?
If the files
Trans.handSpace.his included in multiple translation units (cpp files – in your case, bothmain.cppandtrans.cpp), you will have defined the variable multiple times, thus breaking the one definition rule.If you want a global, you’ll need to declare the variable as
externand define it in a single implementation file.If you want a copy of the variable for each translation unit (probably not), you can declare it
static.Actually, you didn’t. That’s a definition, not a declaration.