1

I am trying to implement a Stack program which comprises of multiple smaller stack. Like for example:-

|..|
|..|
|..|    |..|    |..|    |..|
|..| = |..| + |..| + |..|
|..|
|..|

In words suppose I wish to implement a stack of size 1000. I 'll use 10 stacks of size 100 each.

Like I have a fixed size of a stack and in case a stack of larger size is required another smaller stack is created.

I have written the following code-

#include<iostream>
#define MAX 2
using namespace std;
class Node{
    int data;
    Node *next;
    Node(int d){
        data = d;
        next = NULL;
    }
};
Node *n = NULL;
class multiStack{
    public:
    Node *head;
    int size = 0;
    void push(int d){
        if(size < MAX){
            if( head == NULL){
                head = new Node(d);
            }else{
                Node *p = new Node(d);
                p->next = head;
            head = p;
            }
            n = head;
            size++;
        }else{
            multiStack Sprime;
            Sprime.head->next = n;
            Sprime.push(d);
        }
    }
    void print(){
        Node *p;
        p = n;
        while (p!=null){
            cout<<p->data<<" ";
            p = p->next;
        }
    }
};
int main(){
    multiStack s;
    s.push(2);
    s.push(1);
    s.push(4);
    s.print();
    return 0;
}

The compiler of codepad.org is giving a "Compilation Termination Error due to -Wfatal-errors" on the following Line of Code-

int size = 0 ;

Thanx in advance If you could solve the error .

Below is the error details -
enter image description here

o12d10
  • 800
  • 3
  • 17
  • 31
  • 1
    Well, what are the "fatal errors?" – OldProgrammer Dec 27 '13 at 00:50
  • when a code is written and compiler falses to associate it with some parent function or parenthesis. Example:- If you are writing if else if(x>y) x++; y++; else{ y++; } This would prompt for a fatal error that else doesn't belong to any if – o12d10 Dec 27 '13 at 00:52
  • 4
    Try saying `int size;` in the class, and then adding a constructor that sets it to 0. – cHao Dec 27 '13 at 00:54
  • You should post the error, but have a look [here](http://stackoverflow.com/questions/15451840/why-cant-we-initialize-class-members-at-their-declaration) –  Dec 27 '13 at 00:54
  • 1
    @nabia I have updated the question with the snapshot of the error of the program. check it out in the question. – o12d10 Dec 27 '13 at 01:04
  • 2
    You can't initialize this data member in the class definition (prior to C++11) - http://stackoverflow.com/questions/15451840/why-cant-we-initialize-class-members-at-their-declaration – OldProgrammer Dec 27 '13 at 01:06

1 Answers1

0

What you are doing is possible in C++11, but not earlier versions. If your compiler supports it, you can add -Wc++11-extensions to your compile flags.

Otherwise, do as @cHao says, and initialize the value to 0 in a constructor or a constructor initialization list.

class multiStack{
    public:
      Node *head;
      int size;

      multiStack() : head(0), size(0) { }

    // the rest of your class
};

You also need to initialize head, which I’ve shown above.

Nate
  • 18,752
  • 8
  • 48
  • 54
  • Thanks . . I did implement the constructor and trying to do the same using the compiler flag. But the problem is I am using codepad.org for compilation. – o12d10 Dec 27 '13 at 01:18
  • Initializing it in the constructor should work. I’ve updated the code to address the warnings generated by codepad.org. You still have the problem that `Node` has a private constructor, but that’s easy to fix (or open another question). – Nate Dec 27 '13 at 01:22