I cannot figure out these errors can someone please help

error C2065: 'stack' : undeclared identifier
error C2228: left of '.isEmptyStack' must have class/struct/union type
error C2228: left of '.pop' must have class/struct/union type
error C2228: left of '.push' must have class/struct/union type
error C2228: left of '.top' must have class/struct/union type
error C3861: 'stack': identifier not found, even with argument-dependent lookup

here is my code

#include <iostream>
using namespace std;

#ifndef H_stackType
#define H_stackType

template<class Type>
class stackType
{
public:
	const stackType<Type>& operator=(const stackType<Type>&);
	void initializeStack();
	bool isEmptyStack();
	bool isFullStack();
	void push(const Type& newItem);
  	Type top();
	void pop();
	stackType(int stackSize = 100);
	~stackType();

private:
	int maxStackSize;
	int stackTop;
	Type *list;

};

#endif

#include "stackType.h"
#include <iostream>

using namespace std;

template<class Type>
void stackType<Type>::initializeStack()
{
	stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
	return(stackTop == 0);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
	return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
  if (stackTop < stackSize - 1)
  {
    ++stackTop;
    list[stackTop] = newItem;
  }
  else
    cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
  assert(stackTop != 0);
  return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
  if (stackTop >= 0)
    stackTop--;
  else
   cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize) 
{
	if(stackSize <= 0)
	{
		cerr<<"Size of the array to hold the stack must "
			<<"be positive."<<endl;
		cerr<<"Creating an array of size 100."<<endl;

		maxStackSize = 100;
	}
	else
		maxStackSize = stackSize;

	stackTop = 0;	
	list = new Type[maxStackSize];
	assert(list != NULL);
}

template<class Type>
stackType<Type>::~stackType()
{
   delete [] list;
}

#include <iostream>
#include <string>
#include <cassert>
#include "stackType.h"

using namespace std;

string RPN(string infix);


int main()
{
  string infix;
  stackType<string> stack(100);

  cout << "NOTE: Enter ! for infix expression to exit." << endl;
  for (;;)
  {
    cout << "Infix Expression?  ";
    getline(cin, infix);
    if (infix == "!") break;
	cout << "RPN Expression is: " << RPN(infix) << endl;
  } 
  return 0;
}

string RPN(string infix)
{
  char topToken;
  string RPNexp;
  const string BLANK = " ";

  for (unsigned i = 0; i < infix.length(); i++)
  {
    switch(infix[i])
    {
      case ' ' : break;

      case '(' : stack.push(infix[i]);
                 break;

      case ')' : for (;;)
                 {
                   assert (!stack.isEmptyStack());
                   topToken = stack.top();
                   stack.pop();
                   if (topToken == '(') break;

                   RPNexp.append(BLANK + topToken);
                 }
                 break;

      case '+' :  case '-' :
      case '*' :  case '/' :
                 for (;;)
                 {
                   if (stack.isEmptyStack() ||
                       stack.top() == '(' ||
                       (infix[i] == '*' || infix[i] == '/') &&
                       (stack.top() == '+' ||  stack.top() == '-')
                      )
                   {
                     stack.push(infix[i]);
                     break;
                   }
                   else
                   {
                     topToken = stack.top();
                     stack.pop();
                     RPNexp.append(BLANK + topToken);
                   }
                 }
                 break;

      default :  RPNexp.append(BLANK + infix[i]);
    }
  }

  for (;;)
  {
    if (stack.isEmptyStack()) break;

    topToken = stack.top();
    stack.pop();
    if (topToken != '(')
    {
      RPNexp.append(BLANK + topToken);
    }
    else
    {
      cout << "Error in infix expression";
      break;
    }
  }
  return RPNexp;
}

Recommended Answers

All 11 Replies

You're using stack in RPN() as if it was a global variable, when in fact it is local to main().

You're using stack in RPN() as if it was a global variable, when in fact it is local to main().

im not sure i know what you mean. i am new to c++ can you elaborate.

Certainitly.

int main()
{
  string infix;
  stackType<string> stack(100);  [b]// stack declared here[/b]

  cout << "NOTE: Enter ! for infix expression to exit." << endl;
  for (;;)
  {
    cout << "Infix Expression?  ";
    getline(cin, infix);
    if (infix == "!") break;
	cout << "RPN Expression is: " << RPN(infix) << endl;
  } 
  return 0;
}

string RPN(string infix)
{
  char topToken;
  string RPNexp;
  const string BLANK = " ";

  for (unsigned i = 0; i < infix.length(); i++)
  {
    switch(infix[i])
    {
      case ' ' : break;

      case '(' : stack.push(infix[i]);  [b]// ... but referenced in here![/b]
                 break;

      case ')' : for (;;)
                 {
                   assert (!stack.isEmptyStack());
                   topToken = stack.top();
                   stack.pop();
                   if (topToken == '(') break;

                   RPNexp.append(BLANK + topToken);
                 }
                 break;

    // ... more uses of stack
}

The solution: move stackType<string> stack(100); outside of main (just above it in the source file), or pass it as an argument.

great that worked and taken care of the prvious errors however now i have 53 new errors.

error C2440: '=' : cannot convert from 'std::string' to 'char'
error C2664: 'stackType<Type>::push' : cannot convert parameter 1 from 'std::allocator<_Ty>::value_type' to 'const std::string &'
        with
        [
            Type=std::string
        ]
        and
        [
            _Ty=char
        ]
error C2676: binary '==' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const T1 *' from 'std::string'

do you know what these errors mean?

53? Uh oh.

error C2440: '=' : cannot convert from 'std::string' to 'char'

Use array brackets.

"use array brackets" how would i do that? as i said before i am new to c++ and my book does not provide the help i need

alright I simplified my code just to get it to compile however now I get only 2 errors and i do not know why

error LNK2020: unresolved token (0A00001B) stackType<char>.__dtor

fatal error LNK1120: 1 unresolved externals

can you please look at my code and tell me what I am doing wrong?

#ifndef H_stackType
#define H_stackType

template<class Type>
class stackType
{
public:
	void initializeStack();
	bool isEmptyStack();
	void push(const Type& newItem);
  	Type top();
	void pop();
	stackType(int stackSize = 50);
	~stackType();

private:
	int maxStackSize;
	int stackTop;
	Type *list;

};

#endif

#include "stackType.h"
#include <iostream>

using namespace std;

template<class Type>
void stackType<Type>::initializeStack()
{
	stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
	return(stackTop == 0);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
  if(!isFullStack())
    {
		list[stackTop] = newItem;
		stackTop++;
    }
    else
		cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
  assert(stackTop != 0);
  return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
  if(!isEmptyStack())
	   stackTop--;
 	else
	   cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize) 
{
	if(stackSize <= 0)
	{
		cerr<<"Size of the array to hold the stack must "
			<<"be positive."<<endl;
		cerr<<"Creating an array of size 50."<<endl;

		maxStackSize = 50;
	}
	else
		maxStackSize = stackSize;

	stackTop = 0;	
	list = new Type[maxStackSize];
	assert(list != NULL);
}

template<class Type>
stackType<Type>::~stackType()
{
   delete [] list;
}

#include <iostream>
#include <string>
#include "stackType.h"

using namespace std;

string rpn(string infix);


int main()
{
  string infix;
  
  cout << "NOTE: Enter ! for infix expression to exit." << endl;
  for (;;)
  {
    cout << "Infix Expression?  ";
    getline(cin, infix);
    if (infix == "!") break;
	cout << "RPN Expression is: " << rpn(infix) << endl;
  } 
  return 0;
}

string rpn(string infix)
{
  char stackOpr;
  stackType<char> stack(50);
  stack.initializeStack();
  for (unsigned i = 0; i < infix.length(); i++)
  {
    switch(infix[i])
	{
      case ' ' : break;

      case '(' : stack.push(infix[i]);
                 break;

      case ')' : stackOpr = stack.top();
						stack.pop();
						if(!stack.isEmptyStack())
							{
								stackOpr = stack.top();
								stack.pop();
							}
							else
								break;

      case '+' :  case '-' : 
      case '*' :  case '/' : 
                 
                 break;

      default :  ;
    }
  }
}
void stackType<Type>::push(const Type& newItem)
{
  if(!isFullStack()) // You havent defined this member function.
    {
		list[stackTop] = newItem;
		stackTop++;
    }
    else
		cerr<<"Cannot add to a full stack."<<endl;
}

ALso

string rpn(string infix)

is not returning a string value.

made the corrections but am still receiving the same error messages

Well I tried building your project. Try putting all the function definitions in the same source file and the header seperate. If the errors I told you about in the previous reply are corrected, the project builds and runs okay. I am still searching for the reason for build failure for multiple source files. Will let you know if I find the solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.