I have been trying to make sense of initializing static class members in c++ for hours now. Conceptually, it seems simple. Static members belong to the class, not the object and therefore need to be accessed with the scope operator. Also, within the class static member functions can only access other static members/member functions.
I tried dozens of permutations of simple code to get it to compile and began began searching stack overflow and other tutorial sites. I've plugged in dozens of examples such as:
class X {
public:
static int x;
static void f(int);
};
void X::f(int z) {x=z;}
int main(){}
Nothing compiles. If they are all wrong. They are all consistently wrong. I am getting an undefined reference to 'X::x'. This is about as simple of an example as I have found. Is there a syntax error, or could there be a compiler issue?
Compiler info:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release
Thread model: posix
gcc version 7.1.1 20170621 (GCC)