Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ HELP . . I keep getting these errors and warnings. Please help me debug my program! . ERRORS: Severity Code Description Project File Line

C++ HELP

.

.

I keep getting these errors and warnings. Please help me debug my program!

.

ERRORS:

Severity Code Description Project File Line Suppression State

Error C2061 syntax error: identifier 'StackNode' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2061 syntax error: identifier 'StackNode' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2061 syntax error: identifier 'StackNode' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2244 'StackLinked::StackNode::StackNode': unable to match function definition to an existing declaration lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Error C2244 'StackLinked::StackNode::StackNode': unable to match function definition to an existing declaration lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Error C2244 'StackLinked::StackNode::StackNode': unable to match function definition to an existing declaration lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Warning C4346 'StackLinked::StackNode': dependent name is not a type lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Warning C4346 'StackLinked::StackNode': dependent name is not a type lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Warning C4346 'StackLinked::StackNode': dependent name is not a type lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

.

Header Files

.

config.h

// Laboratory 6 config.h

//

//--------------------------------------------------------------------

/**

* Stack class (Lab 6) configuration file.

* Activate test #N by defining the corresponding LAB6_TESTN to have the value 1.

*/

#define LAB6_TEST1 1 // 0 => use array implementation, 1 => use linked impl.

.

Stackh.h

#ifndef STACK_H

#define STACK_H

#include

#include

using namespace std;

template

class Stack {

public:

static const int MAX_STACK_SIZE = 8;

virtual ~Stack();

virtual void push(const DataType& newDataItem) throw (logic_error) = 0;

virtual DataType pop() throw (logic_error) = 0;

virtual void clear() = 0;

virtual bool isEmpty() const = 0;

virtual bool isFull() const = 0;

virtual void showStructure() const = 0;

};

template

Stack::~Stack()

// Not worth having a separate class implementation file for the destuctor

{}

#endif // #ifndef STACK_H

.

StackArray.h

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include

#include

using namespace std;

#include "Stack.h"

template

class StackArray : public Stack {

public:

StackArray(int maxNumber = Stack::MAX_STACK_SIZE);

StackArray(const StackArray& other);

StackArray& operator=(const StackArray& other);

~StackArray();

void push(const DataType& newDataItem) throw (logic_error);

DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;

bool isFull() const;

void showStructure() const;

private:

int maxSize;

int top;

DataType* dataItems;

};

#endif //#ifndef STACKARRAY_H

.

StackLinked.h

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include

#include

using namespace std;

#include "Stack.h"

template

class StackLinked : public Stack {

public:

StackLinked(int maxNumber = Stack::MAX_STACK_SIZE);

StackLinked(const StackLinked& other);

StackLinked& operator=(const StackLinked& other);

~StackLinked();

void push(const DataType& newDataItem) throw (logic_error);

DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;

bool isFull() const;

void showStructure() const;

private:

class StackNode {

public:

StackNode(const DataType& nodeData, StackNode* nextPtr);

DataType dataItem;

StackNode* next;

};

StackNode* top;

};

#endif //#ifndef STACKARRAY_H

.

Source Files

.

StackArray.cpp

#include

#include "StackArray.h"

//--------------------------------------------------------------------

// Creates an empty stack.

template

StackArray::StackArray(int maxNumber) : maxSize(maxNumber), top(-1)

{

dataItems = new DataType[maxNumber];

}

// Copy constructor for linked stack

template

StackArray::StackArray(const StackArray& other) : maxSize(other.maxSize), top(other.top)

{

dataItems = new DataType[maxSize];

for (int i = 0; i <= top; i++)

{

dataItems[i] = other.dataItems[i];

}

}

// Overloaded assignment operator for the StackArray class.

template

StackArray& StackArray::operator=(const StackArray& other)

{

// Self-assignment protection

if (this != &other) return *this;

if (maxSize < other.maxSize)

{

// This object's array is smaller than the other object's array.

// Make an equally big array.

delete[] dataItems;

dataItems = new DataType[other.maxSize];

}

// Now proceed to copy state data over from other object.

maxSize = other.maxSize;

top = other.top;

for (int i = 0; i <= top; i++)

{

dataItems[i] = other.dataItems[i];

}

return *this;

}

// Frees the memory used by a stack.

template

StackArray::~StackArray()

{

delete[] dataItems;

}

// Inserts newDataItem onto the top of a stack.

template

void StackArray::push(const DataType& newDataItem) throw (logic_error)

{

if (isFull())

{

throw logic_error("push() while stack full");

}

dataItems[top + 1] = newDataItem;

top++;

}

// Removes the topmost data item from a stack and returns it.

template

DataType StackArray::pop() throw (logic_error)

{

if (isEmpty())

{

throw logic_error("pop() while stack empty");

}

DataType temp = dataItems[top];

dataItems[top] = 0;

top--;

maxSize--;

return dataItems[top + 1];

}

// Removes all the data items from a stack

template

void StackArray::clear()

{

top = -1;

}

// Returns true if a stack is empty. Otherwise, returns false.

template

bool StackArray::isEmpty() const

{

return top == -1;

}

// Returns true if a stack is full. Otherwise, returns false.

template

bool StackArray::isFull() const

{

return top == maxSize - 1;

}

// Array implementation. Outputs the data items in a stack. If the

// stack is empty, outputs "Empty stack". This operation is intended

// for testing and debugging purposes only.

template

void StackArray::showStructure() const

{

if (isEmpty())

{

cout << "Empty stack." << endl;

}

else {

int j;

cout << "top = " << top << endl;

for (j = 0; j < maxSize; j++)

cout << j << "\t";

cout << endl;

for (j = 0; j <= top; j++)

{

if (j == top)

{

cout << '[' << dataItems[j] << ']' << "\t"; // Identify top

}

else

{

cout << dataItems[j] << "\t";

}

}

cout << endl;

}

cout << endl;

}

.

StackLinked.h

#include

#include "StackLinked.h"

//--------------------------------------------------------------------

// Creates a stack node containing item newDataItem and next pointer nextPtr

template

StackLinked::StackNode::StackNode(const DataType& newDataItem, StackLinked::StackNode* nextPtr)

{

dataItem = newDataItem;

next = nextPtr;

}

// Creates an empty stack. The parameter maxNumber is provided for

// compatability with the array implementation and is ignored

template

StackLinked::StackLinked(int maxNumber)

{

StackLinked stack = new StackLinked(maxNumber);

DataType dataItem = 0;

StackNode* next = 0;

}

// Copy constructor for linked stack

template

StackLinked::StackLinked(const StackLinked& other) : top(0)

{

(void) operator=(other); // Use operator=, ignore return value

}

// Overloaded assignment operator for the StackLinked class

template

StackLinked& StackLinked::operator=(const StackLinked& other)

{

// Self-assignment protection

if (this != &other) return *this;

clear(); // Clear existing nodes

if (!other.isEmpty()) {

// Copy first node

top = new StackNode(other.top->dataItem, 0);

StackNode *otherTemp = other.top->next;

StackNode *thisTemp = 0, *thisPrevious = top;

// Copy rest of nodes

while (otherTemp != 0)

{

thisTemp = new StackNode(otherTemp->dataItem, 0);

thisPrevious->next = 0;

thisPrevious = top->next;

otherTemp = top;

}

}

return *this;

}

// Destructor. Frees the memory used by a stack

template

StackLinked::~StackLinked()

{

clear();

}

// Inserts newDataItem onto the top of a stack

template

void StackLinked::push(const DataType& newDataItem) throw (logic_error)

{

if (isFull()) {

// Not likely with linked implementation

throw logic_error("push() while stack full");

}

if (isEmpty()) {

top = new StackNode(newDataItem, 0);

}

else {

StackNode* store = top;

top = new StackNode(newDataItem, store);

}

}

// Removes the topmost item from a stack and returns it

template

DataType StackLinked::pop() throw (logic_error)

{

if (isEmpty()) {

throw logic_error("pop() while stack empty");

}

DataType mTop = top->dataItem;

top = top->next;

return mTop;

}

// Removes all the data items from a stack

template

void StackLinked::clear()

{

while (!isEmpty) {

delete this;

}

}

// Returns true if a stack is empty. Otherwise, returns false

template

bool StackLinked::isEmpty() const

{

return top == 0;

}

template

bool StackLinked::isFull() const

{

return false;

}

// Linked list implementation. Outputs the data elements in a stack.

// If the stack is empty, outputs "Empty stack". This operation is

// intended for testing and debugging purposes only.

template

void StackLinked::showStructure() const

{

if (isEmpty())

{

cout << "Empty stack" << endl;

}

else

{

cout << "Top\t";

for (StackNode* temp = top; temp != 0; temp = temp->next) {

if (temp == top) {

cout << '[' << temp->dataItem << "]\t";

}

else {

cout << temp->dataItem << "\t";

}

}

cout << "Bottom" << endl;

}

}

.

test6 (Driver Program)

#include

using namespace std;

#include "config.h"

#if !LAB6_TEST1

# include "StackArray.cpp"

#else

# include "StackLinked.cpp"

#endif

void print_help()

{

cout << endl << "Commands:" << endl;

cout << " H : Help (displays this message)" << endl;

cout << " +x : Push x" << endl;

cout << " - : Pop" << endl;

cout << " C : Clear" << endl;

cout << " E : Empty stack?" << endl;

cout << " F : Full stack?" << endl;

cout << " Q : Quit the test program" << endl;

cout << endl;

}

template

void test_stack(Stack& testStack)

{

DataType testDataItem; // Stack data item

char cmd; // Input command

print_help();

do

{

testStack.showStructure(); // Output stack

cout << endl << "Command: "; // Read command

cin >> cmd;

if (cmd == '+')

cin >> testDataItem;

try {

switch (cmd)

{

case 'H': case 'h':

print_help();

break;

case '+': // push

cout << "Push " << testDataItem << endl;

testStack.push(testDataItem);

break;

case '-': // pop

cout << "Popped " << testStack.pop() << endl;

break;

case 'C': case 'c': // clear

cout << "Clear the stack" << endl;

testStack.clear();

break;

case 'E': case 'e': // isEmpty

if (testStack.isEmpty())

cout << "Stack is empty" << endl;

else

cout << "Stack is NOT empty" << endl;

break;

case 'F': case 'f': // isFull

if (testStack.isFull())

cout << "Stack is full" << endl;

else

cout << "Stack is NOT full" << endl;

break;

case 'Q': case 'q': // Quit test program

break;

default: // Invalid command

cout << "Inactive or invalid command" << endl;

}

}

catch (logic_error e) {

cout << "Error: " << e.what() << endl;

}

} while (cin && cmd != 'Q' && cmd != 'q');

}

int main() {

#if !LAB6_TEST1

cout << "Testing array implementation" << endl;

StackArray s1;

test_stack(s1);

#else

cout << "Testing linked implementation" << endl;

StackLinked s2;

test_stack(s2);

#endif

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Influxdb Database A Comprehensive Guide To Learn Influxdb Database

Authors: Cybellium Ltd ,Kris Hermans

1st Edition

B0CNGGWL7B, 979-8867766450

Students also viewed these Databases questions