private static object initialization, ld errors

Hello, I'm working on a shared stack which can be globally locked by any instance as per an exercise in an old textbook. The problem I am facing is in the push and pop methods for the global stack.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <stack>

/*********************************************************************/
/* shared stack:  stack is shared amongst all instances.  Any instance
   can lock the stack.  Unlock must happen from the same instance that
   locked it.
*/
/*********************************************************************/

using namespace std;

class sh_stack_class {

  private:
    int local_lock;
    static int global_lock;
    static stack<char>pile;

  public:

    sh_stack_class(void);
    void lock(sh_stack_class &st);
    void unlock(void);
    bool test_instance_lock(void);   /* test local lock */
    void push(char);
    char pop(void);

    static bool test_g_lock(void) { /* test global lock */
      return (global_lock == 1);
    }

};

int sh_stack_class::global_lock = 0;

sh_stack_class::sh_stack_class(void) {
  local_lock = 0;
}

void sh_stack_class::lock(sh_stack_class &st) {
  if (!sh_stack_class::test_g_lock()) {
    global_lock = 1;
    st.local_lock = 1;
  }
}

void sh_stack_class::unlock(void) {
  if (local_lock == 1) {
    local_lock = 0;
    global_lock = 0;
  }
}

bool sh_stack_class::test_instance_lock(void) {
  return local_lock;
}

void sh_stack_class::push(char ch) {
  if (local_lock && global_lock) {
    sh_stack_class::pile.push(ch);
  }
}

char sh_stack_class::pop(void) {

  char ch = '\0';

  if (local_lock && global_lock) {
    if (!sh_stack_class::pile.empty()) {
      ch = sh_stack_class::pile.top();
      sh_stack_class::pile.pop();
    }
  }
  return ch;
}

int main () {
  return 0;
}


When compiled I get linker errors:

~/src$ g++ sharestack.cpp
/tmp/ccr46qCR.o: In function `sh_stack_class::pop()':
sharestack.cpp:(.text+0x22d): undefined reference to `sh_stack_class::pile'
sharestack.cpp:(.text+0x240): undefined reference to `sh_stack_class::pile'
sharestack.cpp:(.text+0x252): undefined reference to `sh_stack_class::pile'
/tmp/ccr46qCR.o: In function `sh_stack_class::push(char)':
sharestack.cpp:(.text+0x28a): undefined reference to `sh_stack_class::pile'
collect2: ld returned 1 exit status


I think the problem is either in my declaration of pile or I need to initialize it somehow. Could someone point me in the right direction?
On line 35 you have defined the global_lock static member. You need to do this with all the static member variables, including pile.
Thanks, that seems to have done the trick. I added a
 
stack<char> sh_stack_class::pile;

on the next line and my tests worked perfectly.
Topic archived. No new replies allowed.