Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link error in C++ by g++

Tags:

c++

g++

linker

Please take a look at the program below. Why am I getting an error?

#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 conjunction, 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
like image 513
Sajad Bahmani Avatar asked Dec 29 '22 03:12

Sajad Bahmani


1 Answers

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

/* static */
int serverData::serverTemp;
int serverData::server;
like image 80
Roger Lipscombe Avatar answered Dec 31 '22 16:12

Roger Lipscombe