Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ HELP . . Problem: I don't know why, but I keep getting these errors. It's getting really frustrating! I would appreciate the help. Errors

C++ HELP

.

.

Problem: I don't know why, but I keep getting these errors. It's getting really frustrating! I would appreciate the help.

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 C2244 'StackLinked::StackNode::StackNode': unable to match function definition to an existing declaration lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Error C2672 'test_stack': no matching overloaded function found lab6 c:\users\user\desktop\lab6\lab6\test6.cpp 109

Error C2784 'void test_stack(Stack &)': could not deduce template argument for 'Stack &' from 'StackLinked' lab6 c:\users\user\desktop\lab6\lab6\test6.cpp 109

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C3083 'StackNode': the symbol to the left of a '::' must be a type lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 19

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 20

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 28

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 28

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 28

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 29

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 29

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 38

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 38

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 38

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 38

Error C2448 'top': function-style initializer appears to be a function definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 39

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 45

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 45

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 45

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 46

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 46

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 72

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 72

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 72

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 73

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 73

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 79

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 79

Error C2182 'StackLinked': illegal use of type 'void' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 79

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 79

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 80

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 80

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 97

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 97

Error C2371 'StackLinked': redefinition; different basic types lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 97

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 97

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 98

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 98

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 111

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 111

Error C2182 'StackLinked': illegal use of type 'void' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 111

Error C2374 'StackLinked': redefinition; multiple initialization lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 111

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 111

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 112

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 112

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 120

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 120

Error C2371 'StackLinked': redefinition; different basic types lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 120

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 120

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 121

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 121

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 127

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 127

Error C2371 'StackLinked': redefinition; different basic types lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 127

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 127

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 128

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 128

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 136

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 136

Error C2182 'StackLinked': illegal use of type 'void' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 136

Error C2374 'StackLinked': redefinition; multiple initialization lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 136

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 136

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 137

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\stacklinked.cpp 137

Error C2995 'void StackArray::showStructure(void) const': function template has already been defined lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 45

Error C2988 unrecognizable template declaration/definition lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 50

Error C2143 syntax error: missing ';' before '<' lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 50

Error C2182 'StackLinked': illegal use of type 'void' lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 50

Error C2374 'StackLinked': redefinition; multiple initialization lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 50

Error C2059 syntax error: '<' lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 50

Error C2143 syntax error: missing ';' before '{' lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 56

Error C2447 '{': missing function header (old-style formal list?) lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 56

Error C2001 newline in constant lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 66

Error C2015 too many characters in constant lab6 c:\users\user\desktop\lab6\lab6\show6.cpp 66

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

.

Header Files

.

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.

.

Stack.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

.

StackLinked.h

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include

#include

using namespace std;

#include "Stack.h"

// ADD YOUR CODE HERE (uncomment and complete the 2nd line below)

template

class StackLinked {

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;

// ADD YOUR CODE HERE (need to declare one more member function)

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

.

StackLinked.cpp

#ifndef STACKLINKED_CPP

#define STACKLINKED_CPP

#include

#include "StackLinked.h"

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

template

StackLinked::StackNode::StackNode(const DataType& newDataItem,

StackLinked::StackNode* nextPtr)

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

// nextPtr.

{

dataItem = newDataItem;

next = nextPtr;

}

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

template

StackLinked::StackLinked(int maxNumber)

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

// compatability with the array implementation and is ignored.

{

// ADD YOUR CODE HERE

StackLinked stack = new StackLinked(maxNumber);

DataType dataItem = 0;

StackNode* next = 0;

}

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

template

StackLinked::StackLinked(const StackLinked& other)

// Copy constructor for linked stack

: top(0)

{

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

}

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

template

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

// Overloaded assignment operator for the StackLinked class.

// Because this function returns a StackLinked object reference,

// it allows chained assignment (e.g., stack1 = stack2 = stack3).

{

// 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);

// ADD YOUR CODE HERE (uncomment and complete the following 3 lines)

thisPrevious->next = 0;

thisPrevious = top->next;

otherTemp = top;

}

}

return *this;

}

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

template

StackLinked::~StackLinked()

// Destructor. Frees the memory used by a stack.

{

clear();

}

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

template

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

// Inserts newDataItem onto the top of a stack.

{

if (isFull()) {

// Not likely with linked implementation

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

}

// ADD YOUR CODE HERE

if (isEmpty()) {

top = new StackNode(newDataItem, 0);

}

else {

StackNode* store = top;

top = new StackNode(newDataItem, store);

}

}

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

template

DataType StackLinked::pop() throw (logic_error)

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

{

if (isEmpty()) {

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

}

// ADD YOUR CODE HERE

DataType mTop = top->dataItem;

top = top->next;

return mTop;

}

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

template

void StackLinked::clear()

// Removes all the data items from a stack.

{

// ADD YOUR CODE HERE

while (!isEmpty) {

delete this;

}

}

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

template

bool StackLinked::isEmpty() const

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

{

return top == 0;

}

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

// ADD YOUR CODE HERE (need to implement the function you added to the .h file)

// NOTE: the function has just one line

template

bool StackLinked::isFull() const {

return false;

}

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

template

void StackLinked::showStructure() const

// 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.

{

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;

}

}

#endif //#ifndef STACKLINKED_CPP

.

test6.cpp (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

}

.

show6.cpp

#include "StackArray.cpp"

#include "StackLinked.cpp"

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

template

void StackArray::showStructure() const

// 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.

{

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;

}

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

template

void StackLinked::showStructure() const

// 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.

{

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;

}

}

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

Step: 3

blur-text-image

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

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions