d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C++ Custom Stack Template Help
Add Reply New Topic New Poll
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Feb 18 2015 01:20pm
I got to do this stack program for class and im having a bit of a problem. We were supposed to use a custom "stack.h" template to make a stack and use it to find the result of an infix equation.
I used the normal c++ standard template library ( #include<stack>)
and when i changed it to the template that we have to use i keep getting an error.
I havn't worked with custom templates, so im a bit lost on this area.
Basically what should i do to make this actual code work with using the custom template
If i switch the code back to #include<stack> the code works perfectly.



Code
// Test data : 123*+4-
// answer should be : 3
// for problem 15, first change infix to postfix

#include<iostream>
#include "stack.h"
#include<string>
using namespace std;


int evaluate(int operand_1, int operand_2, char operate);
int evaluate_xpression(char ptr[], int size);


int main() {


string temp;
int size =0;// size of the array,

cout << " Please enter the postfix expression that you would like to evaluate" << endl;
getline( cin, temp);

char *ptr;
ptr = new char[temp.length()]; // creates a dynamic array based on the number of chars in the string input



for ( int i=0; i< temp.length(); i++)
{
ptr[i]=temp[i];
size +=1;
};





int answer = evaluate_xpression(ptr, size);
cout<<" The Answer to the expression is :"<<answer << endl;

delete[] ptr;

return 0;
}

int evaluate(int operand_1, int operand_2, char operate) {
switch (operate)
{
case '*':
return operand_2 * operand_1;
case '/':
return operand_2 / operand_1;
case '+':
return operand_2 + operand_1;
case '-':
return operand_2 - operand_1;
default :
return 0;
}
}
int evaluate_xpression(char ptr[], int size) {
Stack<int> stack;
int i = 0;
char character;
int answer = 0;
while (i < size)
{
character = ptr[i];
if (isdigit(character))
{

Stack.push(character-'0');
}
else
{

int operand_1 = stack.top();
stack.pop();
int operand_2 = stack.top();
stack.pop();
answer = evaluate(operand_1, operand_2, character);
stack.push(answer);
}
i++;
}
return answer;
}




The "stack.h" header file looks like this

Code
// stack.cpp -- the function definitions for the array implementation of a stack

template <class DataType>
Stack<DataType>::Stack( )
: elements( 2 ), top( -1 )
{
}

template <class DataType>
void Stack<DataType>::push( DataType elementToPush )
{
if ( ++top == elements.length( ) )
elements.changeSize( elements.length( ) << 1 );
elements[ top ] = elementToPush;
}

// removes an element from the top of the stack and returns it in poppedElement;
// returns false if called on an empty stack; otherwise, returns true
template <class DataType>
bool Stack<DataType>::pop( DataType & poppedElement )
{
if ( top == -1 )
return false;

poppedElement = elements[ top ];
top--;

int trysize = elements.length( );
while ( ( top + 1 <= trysize >> 2 ) && trysize > 2 )
trysize >>= 1;

if ( trysize < elements.length( ) ) {
try {
elements.changeSize( trysize );
}
catch( ... ) { }
}

return true;
}

// returns the element at the top of the stack in topElement without removing it
// returns false if called on an empty stack; otherwise, returns true
template <class DataType>
bool Stack<DataType>::peek( DataType & topElement )
{
if ( top == -1 )
return false;
topElement = elements[ top ];
return true;
}

template <class DataType>
bool Stack<DataType>::isEmpty( ) const
{
return top == -1;
}

template <class DataType>
void Stack<DataType>::makeEmpty( )
{
top = -1;
try {
elements.changeSize( 2 );
}
catch( ... ) { }
}



Im getting an error on line 63, that says" use of undeclared identifier stack";

Code
nt evaluate_xpression(char ptr[], int size) {
Stack<int> stack; <--------- This line
int i = 0;
char character;
int answer = 0;
while (i < size)
{


This post was edited by Pino38 on Feb 18 2015 01:21pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 19 2015 03:55am
It seems you accidentally posted stack.cpp instead of stack.h. The problem with templates is that the compiler has to see all used function definitions at the compilation time, otherwise required template instantiations won't be generated and the linker will barf. So: remove stack.cpp, move everything to stack.h.

Btw: using naked new/delete is incorrect idiomatic C++ for quite some years now, if you absolutely have to have dynamically allocated memory, try std::vector<char> or std::shared_ptr<char[]>. In your case, though, you can simply say evaluate_xpression(temp.c_str(), temp.size()+1); or even evaluate_xpression(temp.data(), temp.size()); since you don't care about the ending null.

This post was edited by KrzaQ2 on Feb 19 2015 03:56am
Member
Posts: 19,256
Joined: Mar 24 2008
Gold: 710.00
Feb 19 2015 11:05am
Wow xD I derp'd pretty bad. Thanks for catching that error , I would have been spent like 7 lifetimes trying to figure that out.
Is there any literature/books that can help my c++ code to meet current standards?
The class I am in is using books that are several years old , so i might be committing several crimes with my code.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll