C++ how to reach 'stack' behaviour in this code? - c++

I would like to ask 2 question about this code. Where I just try to simulate a stack.
Stack.h
#pragma once
namespace stackandqueue {
class Stack
{
private:
int index;
int *stackdata;
public:
Stack();
~Stack();
void push(int val);
int pop();
int top();
bool isEmpty();
};
}
Stack.cpp
#include "stdafx.h"
#include "Stack.h"
namespace stackandqueue {
Stack::Stack() : index{ 0 }
{
stackdata = new int[index];
}
Stack::~Stack()
{
delete[] &stackdata;
}
void Stack::push(int val) {
stackdata[index] = val;
index++;
}
int Stack::pop() {
int val = stackdata[index];
index--;
return val;
}
int Stack::top() {
return stackdata[index];
}
bool Stack::isEmpty() {
return index == 0;
}
}
Meaning is to let me create
Stack stack;
And then it initilizes a dynamic array with 0 as first index and that let me push, pop, top values.
First question:
Why am I having unresolved symbols for method definitions?
Second question:
About 'stackdata', you find is the right way if I want to declare an "array" with dynamic size for this behaviour?
I'm open for improvements and best practices. Im used to programming languagesbut I've never delved into c ++ and I don't want to have bad practices. So you see I am taking it from the begining.
Thanks.
I post solution reached with your help that maybe helps someone.
class Stack
{
private:
int index;
int* stackdata;
public:
Stack(int size);
~Stack();
void push(int val);
int pop();
int top();
bool isEmpty();
};
Stack::Stack(int size)
: index {0}, stackdata{new int[size]}
{
}
Stack::~Stack()
{
delete[] stackdata;
}
void Stack::push(int val) {
stackdata[index] = val;
index++;
}
int Stack::pop() {
index--;
return stackdata[index];
}
int Stack::top() {
return stackdata[index-1];
}
bool Stack::isEmpty() {
return index == 0;
}

There are several problems with this.
An array, dynamically allocated or otherwise, is not a stack/queue/vector. You're creating literally 0 ints. All of your element accesses after that have undefined behaviour. You need your array to grow, i.e. be a vector, e.g. std::vector.
delete[] &stackdata has the wrong level of indirection. You meant delete[] stackdata. You were trying to delete the pointer which was not dynamically allocated.
You're missing copy/move constructors and copy/move assignment operators so as soon as you transport a Stack anywhere it will explode. (The original instance will do a delete[] on the same pointer that the copied/moved instances will!) Read about the rule of three/five/zero.
Other than that, it looks like a stack.
The problem you don't have here is an undefined reference, which is funny because that's the only one you asked about. :) If you do indeed have such a thing, it's likely a problem with your build system (failing to compile that source file), which we cannot see.

Related

C++ Creating a copy constructor for stack class

I have defined a stack class containing methods for pushing and popping values onto the stack.
In the tester file (shown below), after running it, an occur occurs & the program crashes. I know this is due to the function f, which creates an error as two pointers are pointing to the same location in memory. If i comment out the line f(s) when the function is called, the pop & push functions work correctly and the output is correct.
To fix this error, I have been asked to ; Create a copy constructor for this class to fix the above problem.
I'm not really familiar with this, so any help would be appreciated in how to do this. Thanks
Main Test file
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
void f(Stack &a) {
Stack b = a;
}
int main() {
Stack s(2); //declare a stack object s which can store 2 ints
s.push(4); //add int 4 into stack s
//s = [4]
s.push(13); //add int 13 into stack s
//s = [4,13]
f(s); //calls the function f which takes in parameter Stack a , and sets Stack b = to it.
//error here - as 2 pointers point to the same location in memory !
cout << s.pop() << endl; //print out top element(most recently pushed) element.
//so should output 13
return 0;
}
Header File Code
#ifndef STACK_H
#define STACK_H
class Stack {
public:
//constructor
Stack(int size);
//destructor
~Stack();
//public members (data & functions)
void push(int i);
int pop();
private:
//private members (data & functions)
int stck_size;
int* stck;
int top;
};
#endif
Stack.cpp Code
#include "Stack.h"
#include <iostream>
#include <string>
using namespace std;
Stack::Stack(int size){
stck_size = size;
stck = new int[stck_size];
top = 0;
}
Stack::~Stack() {
delete[] stck;
}
void Stack::push(int i) {
if (top == stck_size) {
cout << "Stack overflow." << endl;
return;
}
stck[top++] = i;
}
int Stack::pop() {
if (top == 0) {
cout << "Stack underflow." << endl;
return 0;
}
top--; //decrement top so it points to the last element istead of the empty space at the top.
return stck[top];
}
Copy constructor here is pretty quick-and dirty:
Stack::Stack(const Stack & src):
stck_size(src.stack_size),
stck(new int[stck_size]),
top(src.top) //Member Initializer List
{
// copy source's stack into this one. Could also use std::copy.
// avoid stuff like memcpy. It works here, but not with anything more
// complicated. memcpy is a habit it's just best not to get into
for (int index = 0; index < top; index++)
{
stck[index] = src.stck[index];
}
}
Now that you have a copy constructor, you're still likely screwed because the Rule of Three has not been satisfied. You need operator=. And this is easy because the copy construct and the copy and swap idiom makes it easy.
Basic form:
TYPE& TYPE::operator=(TYPE rhs) //the object to be copied is passed by value
// the copy constructor makes the copy for us.
{
swap(rhs); // need to implement a swap method. You probably need one
//for sorting anyway, so no loss.
return *this; // return reference to new object
}
Your copy constructor should look like this:
Stack::Stack(const Stack &r) {
stck_size = r.stck_size;
stck = new int[stck_size];
top = r.top;
memcpy(stck, r.stck, top*sizeof (int));
}

stack is being reset after each push and shows empty message

i have a program with 3classes.
first one is for defining a node(my node is an array with 9 elements) - 2nd one contains some functions - 3rd is defining a static stack(I have a stack with 100members that each member is an array with 9members)
suppose that in main(), I call one of the functions from 2nd class(for example expand() ). expand function is supposed to push a node into stack(push into UN) and update the stack pointer. After that for example I want to have access to top node of stack and pop that node using main(). but I'm successful. when I watch the UN and top node through debug tool, I see that their amount is being reset after each push(stack doesn't accept new elements). whats wrong?
here is some parts of code that is needed:
#include<iostream>
using namespace std;
#define max 100
class node
{
public:
int node_n[9];
friend class func;
friend class stack;
};
node n;
class node;
class func
{
public:
func();
void expand(node,stack);
friend class stack;
};
class node;
class stack
{
private:
int sp;//stack pointer
public:
node un[max];//saves expanded noded(children)
stack();
int isempty(); //this will show whether stack is empty or not
int isfull(); //this will show whether stack is full or not
void push(node);
node pop();
};
//****************************
stack::stack()
{
sp=-1;
}
//****************************
int stack::isempty()
{
if(sp==-1)
return true;
else
return false;
}
//****************************
int stack::isfull()
{
return sp==max-1;
}
//****************************
node stack::pop() //un=un-[n]
{
for(int k=0;k<=8;k++)
n.node[k]=un[sp].node[k];
sp--;
return n;
}
//****************************
void stack::push(node n ) //un=un+{x1....xn}
{
sp++;
for(int k=0;k<=8;k++)
un[sp].node[k]=n.node[k];
}
//****************************
void func::expand(node n,stack st)
{
if ( n.node_n[0]==0 )
{
if(n.node_n[1]==0)
{
n.node_n[0]=1;
n.node_n[1]=1;
st.push(n);
.
.
.
//******************************
int main()
{
func b;
stack st;
node n2;
node s; //initial state
node g; //goal state
for(int h=0;h<=8;h++)
{
s.node[h]=0;
g.node[h]=1;
}
//n2=s;
st.push(s);
Lable1:
n2=st.pop();
b.expand(n2,st);
goto Lable1;
system("pause");
return(0);
}
This function
void func::expand(node n,stack st)
is taking the st parameter by value, meaning that it has its own copy of st and any changes it makes will only affect that copy.
What you probably want is to pass st by reference, so that the function can make changes to the original passed in object. To do this, change the function declaration and definition to:
void func::expand(node n,stack &st)

How to show all elements in dynamic stack and queue (c++)

i need to show all elements of a stack and queue, using static structures i used a recursive function, but with dynamic it doesnt works well at all.
When i use the functions, it shows the elements correctly, but after that, whatever i do, the program crashes.
Also, in order to do the print(), a requeriment is that it suppose that i can only have acces to the top, so if i show the top, i cant see the previous node, unless i pop the current top, then show the new top.
This is the code for the dynamic stack:
class Person{
public:
string nombre,weight;
Person *sig;
public:
void Capture();
void Show();
};
typedef Person *pPerson;
class stack{
public:
pPerson top;
void Push();
void PushPtr(pPerson object);
void Pop();
pPerson Top();
void Print();
};
//Push new elements
void stack::Push(){
pPerson newP;
newP=new Person();
newP->Capture();
if(top==NULL){
newP->next=NULL;
top=newP;
}
else{
newP->next=top;
top=newP;
}
size++;
}
//For print
void Stack::Print(){
if(Empty()){
return;
}
pPerson x=Top();
Pop();
Print();
PushPtr(x);
}
//Function to recieve the "x" pointer
void Stack::PushPtr(pPerson object){
pPerson newP;
newP=object;
if(size==0){
newP->next=NULL;
top=newP;
}
else{
newP->next=top;
top=newP;
}
size++;
}
As i said, the queue is doing the same thing, but figuring out whats the problem here, im pretty sure i'll fix the queue :-/
Thanks in advance.
Stack walks are usually done this way:
void Stack::Print(){
pPerson cur = top;
while (cur != nullptr) {
cur->printIt(); // other print function.
cur = cur->next;
}
}
Unless there is some existential reason to pop the stack and push it back on.

remove a cell from array give me run time error

i have a template array consist Cells that holds the data, described in the code:
template <class T>
class Array
{
private:
//the array is consist cellss that holds the data
template<class S>
class Cell
{
public:
//members:
S* m_data;
//methods:
//C'tor:(inline)
Cell(S* data=NULL): m_data(data){};
//D'tor:(inline)
~Cell(){delete m_data;};
//C.C'tor:(inlnie)
Cell(const Cell<S>& cell): m_data(cell.m_data){};
};
private:
//members of Array:
Cell<T>* m_head,*m_last;
unsigned int m_size;
public:
/*******************C'tors and D'tors************************/
//C'tor:(inline)
Array():m_head(NULL),m_last(NULL), m_size(0){};
//D'tor:
~Array(){delete[] m_head;};
//C.C'tor:
Array(const Array& array): m_head(array.m_head),m_last(array.m_last),m_size(array.m_size){};
/****************Adding********************/
//add an element to the end of the Array:
void add(const T added);
/*******************Returning and deleting***********************/
T& operator[](const unsigned int index)const {return *(m_head[index].m_data);};
//delete the last element:
void remove();
/*********************Size*****************************/
//returning the number of elements:(inline)
const unsigned int size()const{return m_size;};
//check if the Array is empty:
bool isEmpty()const {return (m_size==0);};
};
now this is the implementaion of add:(after tests look like it works fine but just for case i write it here also)
template <class T>void Array<T>::add(const T added)
{
//allocating memory for the new array:
Cell<T>* newarray=new Cell<T>[m_size+1];
//copy all the elements from the old array:
unsigned int i;
for (i=0; i<m_size;i++)
newarray[i].m_data=m_head[i].m_data;
//put the added in the last index:
T* newelement= new T(added);
newarray[i].m_data=newelement;
//change the ptrs:
m_head=newarray;
m_last=&newarray[m_size];
//increase the counter:
m_size++;
}
and this is the implementaion of remove:
template <class T>void Array<T>::remove()
{
//if there is only 1 element:
if(m_size==1)
{
delete[] m_head;
m_head=m_last=NULL;
}
//change the last the previus cell
else
{
delete m_last;
m_last=m_last-1;
}
//and decrease the counter:
m_size--;
}
now when do:
Array<int> a;
a.add(3);//work fine
a.add(4);//work fine
a.remove();//fail
i get a runtime error from the line delete m_last; even though m_last point to a cell that actually hold data (m_last point to a cell holds 4).
what am i missing here? why cant i delete a pointer to a cell in the Array?
the error VS2012 give me:_BLOCK_TYPE_IS_VAILED(pHead->nBlockUse)
another important thing i forgot to say: when debug it doesnt enter the D'tor of Cell at all, it just get out when going to the delete.
You can't delete one element of array.
int *x = new int[10];
delete &x[2] ; // It is incorrect!
You can only delete whole of array:
delete [] x;
The destructor ~Cell calls delete. That's a sure sign that the constructor should call new.

C++ Visual Studio 2010, compile error C3867 when implementing dynamic stack

Currently working on an assignment for my datastructures class in university using stacks with dynamic memory allocation. Currently I'm getting a compile error C3867 saying I'm missing a function call from an argument list. I'm not really understanding where this error is coming from / I'm having trouble identifying what exactly is my error in my code; so I was wondering if someone might be kind enough to explain to me what it is, and maybe a friendly tip to remember so I can not have this happen again.
also, I apologize for the poor formatting, I've never posted here before sorry if its hard to read. :(
code posted below.
Thanks, and regards. :P
Header File:
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include <iomanip>
struct Node
{
Node *nextPtr;
int value;
};
class Stack
{
public:
//Constructors and Deconstructers Here
Stack(); //Default Constructor
~Stack(); //Default Deconstructor
//logical methods || functions here
bool isEmpty(void); //prototype checks if stack is empty
//stack operations || function prototypes here
void push(int); //prototype to push values of type int onto the stack
int pop(); //prototype to pop values off of the stack and return a value
int top(); //prototype to return the top value
private:
Node *topPtr; //pointer to class Node Object, specifically for the top of the stack
};
#endif
Class File:
#include "CPTN278_A3_Stack_Arsenault.h"
using namespace std;
Stack::Stack()
{
topPtr = 0; //set the top pointer equal to zero.
}
Stack::~Stack()
{
while (!Stack::isEmpty())
{
Stack::pop();
}
}
bool Stack::isEmpty()
{
if(top == 0)
{
return true;
}
else
{
return false;
}
}
void Stack::push(int valueTMP)
{
Node *itemPtr = new Node;
itemPtr->nextPtr = topPtr;
itemPtr->value = valueTMP;
topPtr = itemPtr;
return;
}
int Stack::pop()
{
int returnValue; //unintialized int
Node *itemPtr; //unintialized pointer to node
returnValue = topPtr->value;
itemPtr = topPtr;
topPtr = itemPtr->nextPtr;
delete itemPtr;
return returnValue;
}
int Stack::top(void)
{
return topPtr->value; //**this is where my error is being thrown**
}
bool Stack::isEmpty()
{
if(top == 0) // <-- here is the problem
{
return true;
}
else
{
return false;
}
}
top is a function, to check its result you need top(). But I think you should be testing topPtr instead.
Before you had:
bool Stack::isEmpty()
{
if(top == 0) <-- top is a function, did you mean 'topPtr'?
{
return true;
}
else
{
return false;
}
}
Fix:
bool Stack::isEmpty()
{
return topPtr == 0;
}
That's your only build error. I'm not sure why you or the compiler thought it was in the top method. That's perfectly fine. Also there's no need to write:
if (expression_that_is_true_or_false)
return true;
else
return false;
Just do:
return expression_that_is_true_or_false;
I might be borderline preaching style here, but try to get used to understanding expressions this way. Expressions involving logical operators like ==, !=, &&, ||, <, >, etc. evaluate to true or false, so there is no need to do conditional branching only to then return what the expression originally evaluated to in the first place.
Oh and I realize this is homework, but check out std::stack later in your free time if you haven't already.
In function Stack::isEmpty(), something wrong with top.
bool Stack::isEmpty()
{
if(top == 0) // here
{
return true;
}
else
{
return false;
}
}
I think it should be as below:
if(topPtr==0)
...