So I have a namespace thing with extern int variables declared in a header. I'm trying to define them in a .cpp with using namespace thing; to simplify initialization, but it doesn't seem to work the way I expected when trying to define a variable in thing.cpp. What gives?
main.cpp:
#include <cstdio>
#include "thing.hpp"
int main()
{
printf("%d\n%d\n",thing::a,thing::func());
printf("Zero initialized array:\n");
for(int i = 0; i < 10; i++)
printf("%d",thing::array[i]);
return 0;
}
thing.hpp:
#pragma once
namespace thing {
extern int
a,
b,
array[10];
extern int func();
}
thing.cpp
#include "thing.hpp"
using namespace thing;
// I wanted to do the same thing with 'a' for all variables
int a,thing::b,thing::array[10];
int thing::func() {
return 12345;
}
error:
/tmp/ccLbeQXP.o: In function `main':
main.cpp:(.text+0x11): undefined reference to `thing::a'
collect2: error: ld returned 1 exit status
thing::ainstead of justausing thing::a;statements for all the symbols defined in the .cpp file.using namespace thing? If I includeiostreamin the header, andusing namespace stdin the .cpp I can use it's functions/methods without thestdnamespace prefix just finething.cppinnamespace thing { ... }like you did withthing.hpp?