stack errors

what can I do, please somebody help


stack.h:36: error: âint Stack<int>::topâ is private
Pro1.h:59: error: within this context
Pro1.h:60: error: statement cannot resolve address of overloaded function
stack.h:36: error: âint Stack<int>::topâ is private
Pro1.h:61: error: within this context
Pro1.h:62: error: statement cannot resolve address of overloaded function
Pro1.h:72: warning: converting to âintâ from âdoubleâ
stack.h:36: error: âint Stack<int>::topâ is private
Pro1.h:90: error: within this context
stack.h: In function âvoid Convert(const std::string&, std::string&)â:
stack.h:36: error: âint Stack<char>::topâ is private
Pro1.h:206: error: within this context
Pro1.h:206: error: âOperatorStack.Stack<char>::topâ cannot be used as a function
stack.h:36: error: âint Stack<char>::topâ is private
Pro1.h:208: error: within this context
Pro1.h:208: error: âOperatorStack.Stack<char>::topâ cannot be used as a function
Pro1.h:209: error: no matching function for call to âStack<char>::pop()â
stack.h:26: note: candidates are: bool Stack<DataType>::pop(DataType&) [with DataType = char]
Pro1.h:212: error: âclass Stack<char>â has no member named âemptyâ
Pro1.h:213: error: no matching function for call to âStack<char>::pop()â
stack.h:26: note: candidates are: bool Stack<DataType>::pop(DataType&) [with DataType = char]
Pro1.h:219: error: âclass Stack<char>â has no member named âisEemptyâ
stack.h:36: error: âint Stack<char>::topâ is private
Pro1.h:221: error: within this context
Pro1.h:221: error: âOperatorStack.Stack<char>::topâ cannot be used as a function
Pro1.h:222: error: no matching function for call to âStack<char>::pop()â
stack.h:26: note: candidates are: bool Stack<DataType>::pop(DataType&) [with DataType = char]
Post your code
// 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( ... ) { }
}

81,0-1 Bot
for(int m=0;m < exp.size(); m++)
{
check = exp[m];
if(IsOperand(check))
{


}
else if(IsOperator(check))
{
if(check == ')')
{
rb++;
if(IsOperator(exp[m+1]) && (exp[m+1]=='+' || exp[m+1]=='-' || exp[m+1]=='*' || exp[m+1]=='/' || exp[m+1]=='^' || exp[m+1]==')'))
{
m++;
if(exp[m] == ')')
rb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
else if(check == '(')
{
lb++;
if(IsOperator(exp[m+1]) && exp[m+1] =='(')
{
m++;
lb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
else
{
if(IsOperator(exp[m+1]) && exp[m+1] == '(')
{
m++;
lb++;
}
else if(IsOperator(exp[m+1]))
error++;
}
}
else
error++;
}

if(error == 0 && lb==rb)
return(true);
else
return(false);
}

#endif
291,6
Hi
Kevinking I did post the code
Could you please paste the code in one block and between code tags (which you can find on right hand side of the box, it says Format and sign is #) ? It's so hard to read what you have pasted.


Last edited on
#ifndef INC_STACK_H
#define INC_STACK_H
#endif
HI KEVINCHKIN THOSE ARE MY CODES SEPARATE WITH ----- EACH ONES


#if _MSC_VER > 1000
#pragma once
#endif
THIS IS STACK.H
#include <string>
#include "array.cpp"
using namespace std;

template <class DataType>
class Stack
{
public:
Stack( );
void push( DataType 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
bool pop( DataType & poppedElement );

// returns the element at the top of the stack in topElement without removing it
// returns false is called on an empty stack; otherwise, returns true
bool peek( DataType & topElement );
bool isEmpty( ) const; // returns true if the stack is empty;
// otherwise, returns false
void makeEmpty( );
private:
Array<DataType> elements;
int top;

-----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
THIS IS STACK.CPP
#ifndef INC_STACK_H
#define INC_STACK_H
#endif
#if _MSC_VER > 1000
#pragma once
#endif

#include <string>
#include "array.cpp"
using namespace std;

template <class DataType>
class Stack
{
public:
Stack( );
void push( DataType 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
bool pop( DataType & poppedElement );

// returns the element at the top of the stack in topElement without removing it
// returns false is called on an empty stack; otherwise, returns true
bool peek( DataType & topElement );
bool isEmpty( ) const; // returns true if the stack is empty;
// otherwise, returns false
void makeEmpty( );
private:
Array<DataType> elements;
int top;
};
~
[santa_gm@pegasus dEt]$ vi stack.cpp
// stack.cpp -- the function definitions for the array implementation of a stack

#ifndef INC_STACK_CPP
#define INC_STACK_CPP
#endif
#if _MSC_VER > 1000
#pragma once
#endif


#include <string>

#include "stack.h"
using namespace std;

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( ... ) { }
}

-----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------- THIS IS P1.H

topToken = opStack.top;
opStack.pop();
RPNexp.append(BLANK + topToken);
}
}
break;

default : RPN.append(BLANK + token);
}
}

// pop remaining operator on the stack

for(;;)
{
if(opStack.isEmpty()) break;

topToken = opStack.top;
opSatack.pop();

if ( topToken != '(')
{
RPNexp.append(BLANK + topToken);
}

else
{

cout << " ????Error in infix expresion ???\n";
break;
}
}
retunr RPNexp;
}

-----------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
This is P1.CPP

#include<iostream>
#include<string>
#include <cassert>
using namespace std;
#include "stack.h"
#include "p1.h"
string RPN(string exp);

int main()
{
string exp;

cout << "Enter # for Infix expression to stop.\n";
for(;;)
{
cout <<"\nInfix Expression?";
getline(cin,exp);
if(exp =="#")break;
cout << "RPN Expression is " << RPN (exp)<< endl;
}
}
~












HI KEVINCHKIN THOSE ARE MY CODES SEPARATE WITH ----- EACH ONES
hi olove5,

As far as I know, when you use template classes, the implementation of the template class **MUST** be in the same file, the .h file itself, not the case with the code you posted above!

I use template classes quite a lot in my small project, and all implementation of the template classes is located in my .h file.

I think this should solve your problem.
Last edited on
On the quick glance at your code, I don't understand...
Why are you including a .cpp file? You should only include .h file.
closed account (z05DSL3A)
How to: Put code into your postings http://www.cplusplus.com/forum/articles/1624/
Topic archived. No new replies allowed.