| | |
compilation errors
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
I cannot figure out these errors can someone please help
here is my code
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
#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; }
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
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
Certainitly.
The solution: move
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
}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
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
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
great that worked and taken care of the prvious errors however now i have 53 new errors.
do you know what these errors mean?
C++ Syntax (Toggle Plain Text)
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'
53? Uh oh.
Use array brackets.
C++ Syntax (Toggle Plain Text)
error C2440: '=' : cannot convert from 'std::string' to 'char'
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
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
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
string rpn(string infix)
![]() |
Similar Threads
- linked list errors (C++)
- A prayer to the C++ God pleaze help!! (C++)
- needed big time hw due and late (C)
- problem splitting c++ program (C++)
Other Threads in the C++ Forum
- Previous Thread: some C puzzles
- Next Thread: please help me as herry as you can
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






