6 Replies - 223 Views - Last Post: 09 February 2018 - 03:46 PM Rate Topic: -----

#1 Arabeth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 18

Static array based stack problem

Posted 09 February 2018 - 10:53 AM

Could someone help me find what's wrong in these codes? I keep getting compilation errors.

// Stack.h
#include <iostream>

#ifndef STACK 		// lacks #endif
#define STACK

const int STACK_CAPACITY = 128;
typedef int StackElement;

class Stack
{
 public:
  Stack();
  bool empty() const;
  void push(const StackElement & value);
  void display(ostream & out) const;
  StackElement top() const;

  void pop();
 
 private:
  
  StackElement myArray[STACK_CAPACITY];
  int myTop;

}; 



//Stack.cpp
#include <iostream>
using namespace std;

//Stack constructor
Stack::Stack(): myTop(-1){}

bool Stack::empty() const
{ 
   return (myTop == 0);   
}

void Stack::push(const StackElement & value)
{
   if (myTop < STACK_CAPACITY - 1) 
   { 
      myArray[myTop] = value;    
      --myTop;
   }
   else
   {
      cerr << "Stack full - can't add new value...\n"
              "Must increase value of STACK_CAPACITY in Stack.h\n";
      exit(1);
   }
}

void Stack::display(ostream & out) const
{
   for (int i = myTop; i >= 0; i++) i--
      out << myArray[i] << endl;
}

StackElement Stack::top() const
{
      return (myArray[myTop])
}

void Stack::pop()
{
      myTop++;
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Static array based stack problem

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2610
  • View blog
  • Posts: 4,163
  • Joined: 21-June 11

Re: Static array based stack problem

Posted 09 February 2018 - 10:55 AM

At a glance, it looks like you forgot to include stack.h in stack.cpp. If that doesn't solve your problem, please post your error messages here.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5470
  • View blog
  • Posts: 17,022
  • Joined: 25-December 09

Re: Static array based stack problem

Posted 09 February 2018 - 10:56 AM

Quote

I keep getting compilation errors.


Please post these compile errors, all of them, exactly as they appear in your development environment.

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Arabeth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 18

Re: Static array based stack problem

Posted 09 February 2018 - 11:31 AM

OK, I have this in codeblocks now:

/*-- Stack.h ---------------------------------------------------

  This header file defines a Stack data type.
  Basic operations:
    constructor:  Constructs an empty stack
    empty:        Checks if a stack is empty
    push:         Modifies a stack by adding a value at the top
    top:          Retrieves the top stack value; leaves stack unchanged
    pop:          Modifies stack by removing the value at the top
    display:      Displays all the stack elements, starting from top

  Class Invariant:
    1. The stack elements (if any) are stored in positions
       0, 1, . . ., myTop of myArray.
    2. -1 <= myTop < STACK_CAPACITY
--------------------------------------------------------------*/

#include <iostream>

#ifndef STACK
#define STACK

const int STACK_CAPACITY = 128;
typedef int StackElement;

class Stack
{
 public:

  Stack();

  bool empty() const;

  void push(const StackElement & value);

  void display(ostream & out) const;

  StackElement top() const;

  void pop();

 private:

  StackElement myArray[STACK_CAPACITY];
  int myTop;

};



/*-- Stack.cpp-----------------------------------------------------------

   This file implements Stack member functions.
   
-------------------------------------------------------------------------*/

#include <iostream>
using namespace std;

//--- Definition of Stack constructor
Stack::Stack()
: myTop(-1)
{}

bool Stack::empty() const
{
   return (myTop == 0);
}

void Stack::push(const StackElement & value)
{
   if (myTop < STACK_CAPACITY - 1)
   {
myArray[myTop] = value;
      --myTop;

   }
   else
   {
      cerr << "*** Stack full -- can't add new value ***\n"
              "Must increase value of STACK_CAPACITY in Stack.h\n";
      exit(1);
   }
}


void Stack::display(ostream & out) const
{
   for (int i = myTop; i >= 0; i++) i--
      out << myArray[i] << endl;
}

StackElement Stack::top() const
{
      return (myArray[myTop])
}


void Stack::pop()
{
      myTop++;
} 



Compiler Errors -->

Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2610
  • View blog
  • Posts: 4,163
  • Joined: 21-June 11

Re: Static array based stack problem

Posted 09 February 2018 - 11:37 AM

The first error is telling you that you have some non-standard space character on line 52. You should replace it with a normal space. Most or all of the other errors seem to be because you still didn't include Stack.h.

PS: In the future please post error messages as copy-and-pasted text, not screenshots. Screenshots are harder to read and, in this case at least, we can't see which file each error belongs to.
Was This Post Helpful? 1
  • +
  • -

#6 Arabeth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-February 18

Re: Static array based stack problem

Posted 09 February 2018 - 03:33 PM

OK I found out how to include the stack.h file and now I have 6 compiler errors:

/*-- Stack.h ---------------------------------------------------

  This header file defines a Stack data type.
  Basic operations:
    constructor:  Constructs an empty stack
    empty:        Checks if a stack is empty
    push:         Modifies a stack by adding a value at the top
    top:          Retrieves the top stack value; leaves stack unchanged
    pop:          Modifies stack by removing the value at the top
    display:      Displays all the stack elements, starting from top

  Class Invariant:
    1. The stack elements (if any) are stored in positions
       0, 1, . . ., myTop of myArray.
    2. -1 <= myTop < STACK_CAPACITY
--------------------------------------------------------------*/

#include <iostream>

#ifndef STACK
#define STACK

const int STACK_CAPACITY = 128;
typedef int StackElement;

class Stack
{
 public:

  Stack();

  bool empty() const;

  void push(const StackElement & value);

  void display(ostream & out) const;

  StackElement top() const;

  void pop();

 private:

  StackElement myArray[STACK_CAPACITY];
  int myTop;

};

#endif


/*-- Stack.cpp-----------------------------------------------------------

   This file implements Stack member functions.

-------------------------------------------------------------------------*/

#include <iostream>
#include "Stack.h"
using namespace std;

//--- Definition of Stack constructor
Stack::Stack()
: myTop(-1)
{}

bool Stack::empty() const
{
   return (myTop == 0);
}

void Stack::push(const StackElement & value)
{
   if (myTop < STACK_CAPACITY - 1)
   {
myArray[myTop] = value;
      --myTop;

   }
   else
   {
      cerr << "*** Stack full -- can't add new value ***\n"
              "Must increase value of STACK_CAPACITY in Stack.h\n";
      exit(1);
   }
}


void Stack::display(ostream & out) const
{
   for (int i = myTop; i >= 0; i++) i--
      out << myArray[i] << endl;
}

StackElement Stack::top() const
{
      return (myArray[myTop])
}

void Stack::pop()
{
      myTop++;
} 



Compiler Errors ->
||=== Build: Debug in Stack (compiler: GNU GCC Compiler) ===|
Code\2-9-18\Stack\main.cpp|52|error: stray '\240' in program|
Code\2-9-18\Stack\Stack.h|36|error: 'ostream' has not been declared|
Code\2-9-18\Stack\main.cpp||In member function 'void Stack::push(const StackElement&)':|
Code\2-9-18\Stack\main.cpp|33|error: 'exit' was not declared in this scope|
Code\2-9-18\Stack\main.cpp|38|error: prototype for 'void Stack::display(std::ostream&) const' does not match any in class 'Stack'|
Code\2-9-18\Stack\Stack.h|36|error: candidate is: void Stack::display(int&) const|
Code\2-9-18\Stack\main.cpp||In member function 'StackElement Stack::top() const':|
Code\2-9-18\Stack\main.cpp|47|error: expected ';' before '}' token|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5470
  • View blog
  • Posts: 17,022
  • Joined: 25-December 09

Re: Static array based stack problem

Posted 09 February 2018 - 03:46 PM

Remember that the ostream class requires the <fstream> header file, and don't forget that you need to properly scope:: the items from the std::namespace.

Also you really should avoid exit() in most C++ programs, this C function doesn't understand C++ classes and may lead to data corruption.



Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1