I have switched from c to c++ recently for my project. I am facing difficulty in initializing static pointer.
Here is some what similar code in my project
file1 static.h
file2 static.ccCode:#include <iostream> #include<string.h> using namespace std; class shared { static int *ptr; static int a; int b; public: void set(int i) {a=i;} void show(); } ;
when i compile the piece of code i get following errorCode:#include "static.h" int shared::a = 2; // define a int shared::*ptr = NULL; void shared::show() { memcpy(&ptr, &a, sizeof(int)); cout << "This is static a: " << a; cout << "\nThis is ptr = " <<*ptr; cout << "\n"; } int main() { shared x; x.set(1); // set a to 1 x.show(); return 0; }
g++ static.cc
/tmp/cc7dngkw.o: In function `shared::show()':
static.cc:(.text+0x1a): undefined reference to `shared::ptr'
static.cc:(.text+0x4a): undefined reference to `shared::ptr'
collect2: ld returned 1 exit status
Any help would be appreciated. thanks in advance.



LinkBack URL
About LinkBacks


