-1

I have a template class for a stack (on which I can perform push/pop operations).

If I call the doSomething() function via

A::doSomething();

I get an "unresolved external symbol..." error message. How can I create an static stack in my class a on which I can perform push and pop operations?

class A {
    private:
        // stack which can hold 4 integers
        static stack<int, 4> s;

    public:
        static void doSomething() {
            s.push(4);
        }
};

You can see a code snippet here: codeshare.io/arJmmY

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
knowledge
  • 941
  • 1
  • 11
  • 26
  • No idea until you've shown a [mcve], but the combination of templates and linker errors hints at a duplicate of [this question](https://stackoverflow.com/q/495021/3233393). – Quentin Sep 18 '19 at 08:51
  • You can see a code snippet here: https://codeshare.io/arJmmY – knowledge Sep 18 '19 at 08:56
  • 1
    The type has nothing to do with it. You would see the same problem if the member were an `int`. – molbdnilo Sep 18 '19 at 09:04

1 Answers1

3

With

class A {
   private:
      static stack<int, 4> s;

    // ...
};

you declare the static member s of the class A.

You have also to define it.

You have to add

stack<int, 4> A::s;

after the A body.

max66
  • 65,235
  • 10
  • 71
  • 111