Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Help Build a stack using linked list. I am getting these error when I try to

ID: 3731922 • Letter: C

Question

C++ Help

Build a stack using linked list. I am getting these error when I try to declare the stack in main. How do I fix it

Undefined symbols for architecture x86_64:

"MyStack<double>::pop(double)", referenced from:

_main in main.o

"MyStack<double>::push(double const&)", referenced from:

_main in main.o

"MyStack<double>::MyStack()", referenced from:

_main in main.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

//

// MyStack.h

// Assignment 5 - Stack Template

//

///Write a template, MyStack.h, to implement a LIFO stack.

#ifndef MyStack_h

#define MyStack_h

template <typename V>

class MyStack

{

/// Define a structure to be used as the stack item

struct StackNode

{

char ch;

StackNode *next;

};

private:

char ch;

StackNode *top; /// Pointer to top of the stack

int size;

public:

///member functions

  

MyStack( ); ///Class constructor. May have a defaulted parameter

~MyStack(); /// Class destructor

  

void push(const V&); /// Push an element onto the stack

V& peek( ); /// returns a reference to the element at the top of the stack

void pop(V); ///Removes an element from the stack

int setSize( ); /// Return size of the stack. The number of the elements in the list

bool empty( ) const; /// Return true if stack is empty. Returns false if it has elements

void clear( ); /// Remove all items from the stack

  

int getSize(bool);

};

#endif /* MyStack_h */

//

// MyStack.cpp

// Assignment 5 - Stack Template

//

#include "MyStack.h"

#include <iostream>

using namespace std;

//constructor

template <typename V>

MyStack<V>::MyStack(){

   top = nullptr;

}

///destructor

template <typename V>

MyStack<V>::~MyStack(){

clear();

}

///push

template <typename V>

void MyStack< V>::push(const V& element) {

StackNode *newNode = nullptr; // Pointer to a new node

  

   // Allocate a new node and store num there.

   newNode = new StackNode;

   newNode->value = element;

  

   // If there are no nodes in the list

   // make newNode the first node.

   if (empty()) {

   top = newNode;

   newNode->next = nullptr;

   }

   else // Otherwise, insert NewNode before top.

   {

   newNode->next = top;

   top = newNode;

   }

  

size++;

}

///pop

template <typename V>

void MyStack< V>::pop(V element) {

  

   StackNode *temp = nullptr; // Temporary pointer

// First make sure the stack isn't empty.

   if (empty()) {

   cout << "The stack is empty. ";

   }

   else // pop value off top of stack

{

   element = top->value;

   temp = top->next;

   delete top;

top = temp;

}

  

}

///peek

template <typename V>

V &MyStack<V>::peek(){

   StackNode *top;

return top;

}

///Set size

template <typename V>

int MyStack<V>::setSize( ) {

// Get the desired stack size.

   cout << "How big should I make the stack? ";

   cin >> size;

  

   // Validate the size.

while (size < 1)

   {

   cout << "Enter 1 or greater: ";

   cin >> size;

   }

}

///get Size

template <typename V>

int MyStack<V>::getSize(bool update) {

if (!update) {

return this->list_size;

}

int size = 0;

V* temp = this->head;

while (temp) {

size++;

temp = temp->next;

}

this->list_size = size;

return this->list_size;

}

///empty

template <typename V>

bool MyStack<V>::empty( ) const{

bool status;

  

   if (!top)

   status = true;

else

   status = false;

return status;

}

///clear

template <typename V>

void MyStack< V>::clear( ){

struct StackNode *temp;

  

if(!empty()){

temp = top;

/// Scan stack and free all nodes

while(top != NULL){

temp = top;

top = top->next;

delete temp;

}

}

top = NULL;

  

}

#include <iostream>

#include "MyStack.h"

using namespace std;

int main() {

MyStack<double> *theStack;

theStack = new MyStack<double>;

  

  

int x=3;

theStack->push(x); //Error

theStack->pop(x); //Error

/// How do You delcare and use the stack pointer correctly?

Thanks

  

Explanation / Answer

There is a concern

The calling of push function seems fine but the definition of StackNode is a concern

The StackNode is defined as

struct StackNode
    {
        char ch;
        StackNode *next;
    };

Firts of all there should be a value field and its type should be V, like the following:

struct StackNode
    {
        V value;
        StackNode *next;
    };

With the above modifications, your calls should work.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote