compilation errors

Reply

Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

compilation errors

 
0
  #1
Dec 4th, 2005
I cannot figure out these errors can someone please help

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

here is my code

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #ifndef H_stackType
  5. #define H_stackType
  6.  
  7. template<class Type>
  8. class stackType
  9. {
  10. public:
  11. const stackType<Type>& operator=(const stackType<Type>&);
  12. void initializeStack();
  13. bool isEmptyStack();
  14. bool isFullStack();
  15. void push(const Type& newItem);
  16. Type top();
  17. void pop();
  18. stackType(int stackSize = 100);
  19. ~stackType();
  20.  
  21. private:
  22. int maxStackSize;
  23. int stackTop;
  24. Type *list;
  25.  
  26. };
  27.  
  28. #endif
  29.  
  30. #include "stackType.h"
  31. #include <iostream>
  32.  
  33. using namespace std;
  34.  
  35. template<class Type>
  36. void stackType<Type>::initializeStack()
  37. {
  38. stackTop = 0;
  39. }
  40.  
  41. template<class Type>
  42. bool stackType<Type>::isEmptyStack()
  43. {
  44. return(stackTop == 0);
  45. }
  46.  
  47. template<class Type>
  48. bool stackType<Type>::isFullStack()
  49. {
  50. return(stackTop == maxStackSize);
  51. }
  52.  
  53. template<class Type>
  54. void stackType<Type>::push(const Type& newItem)
  55. {
  56. if (stackTop < stackSize - 1)
  57. {
  58. ++stackTop;
  59. list[stackTop] = newItem;
  60. }
  61. else
  62. cerr<<"Cannot add to a full stack."<<endl;
  63. }
  64.  
  65. template<class Type>
  66. Type stackType<Type>::top()
  67. {
  68. assert(stackTop != 0);
  69. return list[stackTop - 1];
  70. }
  71.  
  72. template<class Type>
  73. void stackType<Type>::pop()
  74. {
  75. if (stackTop >= 0)
  76. stackTop--;
  77. else
  78. cerr<<"Cannot remove from an empty stack."<<endl;
  79. }
  80.  
  81. template<class Type>
  82. stackType<Type>::stackType(int stackSize)
  83. {
  84. if(stackSize <= 0)
  85. {
  86. cerr<<"Size of the array to hold the stack must "
  87. <<"be positive."<<endl;
  88. cerr<<"Creating an array of size 100."<<endl;
  89.  
  90. maxStackSize = 100;
  91. }
  92. else
  93. maxStackSize = stackSize;
  94.  
  95. stackTop = 0;
  96. list = new Type[maxStackSize];
  97. assert(list != NULL);
  98. }
  99.  
  100. template<class Type>
  101. stackType<Type>::~stackType()
  102. {
  103. delete [] list;
  104. }
  105.  
  106. #include <iostream>
  107. #include <string>
  108. #include <cassert>
  109. #include "stackType.h"
  110.  
  111. using namespace std;
  112.  
  113. string RPN(string infix);
  114.  
  115.  
  116. int main()
  117. {
  118. string infix;
  119. stackType<string> stack(100);
  120.  
  121. cout << "NOTE: Enter ! for infix expression to exit." << endl;
  122. for (;;)
  123. {
  124. cout << "Infix Expression? ";
  125. getline(cin, infix);
  126. if (infix == "!") break;
  127. cout << "RPN Expression is: " << RPN(infix) << endl;
  128. }
  129. return 0;
  130. }
  131.  
  132. string RPN(string infix)
  133. {
  134. char topToken;
  135. string RPNexp;
  136. const string BLANK = " ";
  137.  
  138. for (unsigned i = 0; i < infix.length(); i++)
  139. {
  140. switch(infix[i])
  141. {
  142. case ' ' : break;
  143.  
  144. case '(' : stack.push(infix[i]);
  145. break;
  146.  
  147. case ')' : for (;;)
  148. {
  149. assert (!stack.isEmptyStack());
  150. topToken = stack.top();
  151. stack.pop();
  152. if (topToken == '(') break;
  153.  
  154. RPNexp.append(BLANK + topToken);
  155. }
  156. break;
  157.  
  158. case '+' : case '-' :
  159. case '*' : case '/' :
  160. for (;;)
  161. {
  162. if (stack.isEmptyStack() ||
  163. stack.top() == '(' ||
  164. (infix[i] == '*' || infix[i] == '/') &&
  165. (stack.top() == '+' || stack.top() == '-')
  166. )
  167. {
  168. stack.push(infix[i]);
  169. break;
  170. }
  171. else
  172. {
  173. topToken = stack.top();
  174. stack.pop();
  175. RPNexp.append(BLANK + topToken);
  176. }
  177. }
  178. break;
  179.  
  180. default : RPNexp.append(BLANK + infix[i]);
  181. }
  182. }
  183.  
  184. for (;;)
  185. {
  186. if (stack.isEmptyStack()) break;
  187.  
  188. topToken = stack.top();
  189. stack.pop();
  190. if (topToken != '(')
  191. {
  192. RPNexp.append(BLANK + topToken);
  193. }
  194. else
  195. {
  196. cout << "Error in infix expression";
  197. break;
  198. }
  199. }
  200. return RPNexp;
  201. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: compilation errors

 
0
  #2
Dec 4th, 2005
You're using stack in RPN() as if it was a global variable, when in fact it is local to main().
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

Re: compilation errors

 
0
  #3
Dec 4th, 2005
Originally Posted by dwks
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: compilation errors

 
0
  #4
Dec 4th, 2005
Certainitly.
int main()
{
  string infix;
  stackType<string> stack(100);  // stack declared here

  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]);  // ... but referenced in here!
                 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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

Re: compilation errors

 
0
  #5
Dec 4th, 2005
great that worked and taken care of the prvious errors however now i have 53 new errors.

  1. error C2440: '=' : cannot convert from 'std::string' to 'char'
  2. error C2664: 'stackType<Type>::push' : cannot convert parameter 1 from 'std::allocator<_Ty>::value_type' to 'const std::string &'
  3. with
  4. [
  5. Type=std::string
  6. ]
  7. and
  8. [
  9. _Ty=char
  10. ]
  11. error C2676: binary '==' : 'std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
  12. 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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: compilation errors

 
0
  #6
Dec 4th, 2005
53? Uh oh.
  1. error C2440: '=' : cannot convert from 'std::string' to 'char'
Use array brackets.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

Re: compilation errors

 
0
  #7
Dec 4th, 2005
"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
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

Re: compilation errors

 
0
  #8
Dec 4th, 2005
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?


  1.  
  2. #ifndef H_stackType
  3. #define H_stackType
  4.  
  5. template<class Type>
  6. class stackType
  7. {
  8. public:
  9. void initializeStack();
  10. bool isEmptyStack();
  11. void push(const Type& newItem);
  12. Type top();
  13. void pop();
  14. stackType(int stackSize = 50);
  15. ~stackType();
  16.  
  17. private:
  18. int maxStackSize;
  19. int stackTop;
  20. Type *list;
  21.  
  22. };
  23.  
  24. #endif
  25.  
  26. #include "stackType.h"
  27. #include <iostream>
  28.  
  29. using namespace std;
  30.  
  31. template<class Type>
  32. void stackType<Type>::initializeStack()
  33. {
  34. stackTop = 0;
  35. }
  36.  
  37. template<class Type>
  38. bool stackType<Type>::isEmptyStack()
  39. {
  40. return(stackTop == 0);
  41. }
  42.  
  43. template<class Type>
  44. void stackType<Type>::push(const Type& newItem)
  45. {
  46. if(!isFullStack())
  47. {
  48. list[stackTop] = newItem;
  49. stackTop++;
  50. }
  51. else
  52. cerr<<"Cannot add to a full stack."<<endl;
  53. }
  54.  
  55. template<class Type>
  56. Type stackType<Type>::top()
  57. {
  58. assert(stackTop != 0);
  59. return list[stackTop - 1];
  60. }
  61.  
  62. template<class Type>
  63. void stackType<Type>::pop()
  64. {
  65. if(!isEmptyStack())
  66. stackTop--;
  67. else
  68. cerr<<"Cannot remove from an empty stack."<<endl;
  69. }
  70.  
  71. template<class Type>
  72. stackType<Type>::stackType(int stackSize)
  73. {
  74. if(stackSize <= 0)
  75. {
  76. cerr<<"Size of the array to hold the stack must "
  77. <<"be positive."<<endl;
  78. cerr<<"Creating an array of size 50."<<endl;
  79.  
  80. maxStackSize = 50;
  81. }
  82. else
  83. maxStackSize = stackSize;
  84.  
  85. stackTop = 0;
  86. list = new Type[maxStackSize];
  87. assert(list != NULL);
  88. }
  89.  
  90. template<class Type>
  91. stackType<Type>::~stackType()
  92. {
  93. delete [] list;
  94. }
  95.  
  96. #include <iostream>
  97. #include <string>
  98. #include "stackType.h"
  99.  
  100. using namespace std;
  101.  
  102. string rpn(string infix);
  103.  
  104.  
  105. int main()
  106. {
  107. string infix;
  108.  
  109. cout << "NOTE: Enter ! for infix expression to exit." << endl;
  110. for (;;)
  111. {
  112. cout << "Infix Expression? ";
  113. getline(cin, infix);
  114. if (infix == "!") break;
  115. cout << "RPN Expression is: " << rpn(infix) << endl;
  116. }
  117. return 0;
  118. }
  119.  
  120. string rpn(string infix)
  121. {
  122. char stackOpr;
  123. stackType<char> stack(50);
  124. stack.initializeStack();
  125. for (unsigned i = 0; i < infix.length(); i++)
  126. {
  127. switch(infix[i])
  128. {
  129. case ' ' : break;
  130.  
  131. case '(' : stack.push(infix[i]);
  132. break;
  133.  
  134. case ')' : stackOpr = stack.top();
  135. stack.pop();
  136. if(!stack.isEmptyStack())
  137. {
  138. stackOpr = stack.top();
  139. stack.pop();
  140. }
  141. else
  142. break;
  143.  
  144. case '+' : case '-' :
  145. case '*' : case '/' :
  146.  
  147. break;
  148.  
  149. default : ;
  150. }
  151. }
  152. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: compilation errors

 
0
  #9
Dec 4th, 2005
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
  1. string rpn(string infix)
is not returning a string value.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 38
Reputation: btech is an unknown quantity at this point 
Solved Threads: 0
btech btech is offline Offline
Light Poster

Re: compilation errors

 
0
  #10
Dec 4th, 2005
made the corrections but am still receiving the same error messages
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC