Thread: static pointer data member

  1. #1
    Registered User
    Join Date
    Mar 2011

    Unhappy static pointer data member

    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
    #include <iostream>
    using namespace std;
    class shared {
    	static int *ptr;
    	static int a;
    	int b;
    	void set(int i) {a=i;}
    	void show();
    } ;
    #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;
    	return 0;
    when i compile the piece of code i get following error
    /tmp/cc7dngkw.o: In function `shared::show()': undefined reference to `shared::ptr' undefined reference to `shared::ptr'
    collect2: ld returned 1 exit status

    Any help would be appreciated. thanks in advance.
    Last edited by suppu143; 03-14-2011 at 09:38 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Read the intro threads to learn about code tags,
    press the edit button at the bottom of your post,
    delele all your code, and then repast your code from your editor, using the code tags.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    I cant say i can spot the problem here, might be something very obvious,,,but hard to see with your non formatted post! One thing to note is that it is best practice to refer to static members by their 'full' names like:
    shared::a += 100;
    for example
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. Can not access static data member.
    By CottonXu in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2005, 11:41 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM