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
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
_main in main.o
"MyStack
_main in main.o
"MyStack
_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
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
using namespace std;
//constructor
template
MyStack
top = nullptr;
}
///destructor
template
MyStack
clear();
}
///push
template
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
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
V &MyStack
StackNode *top;
return top;
}
///Set size
template
int MyStack
// 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
int MyStack
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
bool MyStack
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
///clear
template
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
#include "MyStack.h"
using namespace std;
int main() {
MyStack
theStack = new MyStack
int x=3;
theStack->push(x); //Error
theStack->pop(x); //Error
/// How do You delcare and use the stack pointer correctly?
Thanks
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started