Communication error in C ++ using g ++

Please check out the program below. Why am I getting an error message?

#include <stdlib.h> #include <string> #include <string.h> #include <iostream> using namespace std; class serverData { public: static int serverTemp; static int server; }; int main(int argc, char** argv) { string s = "sajad bahmani"; serverData::server = 90 ; const char * a = s.data(); cout << a[0] << endl; return (EXIT_SUCCESS); } 

In combination, I get this error when trying to link:

 build/Debug/GNU-Linux-x86/main.o: In function `main': /home/sb23/pr/main.cpp:14: undefined reference to `serverData::server' collect2: ld returned 1 exit status 
+4
source share
2 answers

Static member variables must have storage allocated in one of your .CPP files:

 /* static */ int serverData::serverTemp; int serverData::server; 
+8
source

You have just declared your static members inside the class, but you have not defined them yet. You must define them outside the class.

 //definition int serverData::serverTemp; //implicitly initialized to 0 int serverData::server = 5; // initialized to 5 
+2
source

Source: https://habr.com/ru/post/1300298/


All Articles