Mar 27, 2014 at 12:08am UTC
I keep getting the same error messages every time on Visual Studio. I don't know where the error is originating. Basically I'm trying to convert an infix expression (A+B-C) to a postfix expression (AB+C-) using stacks. Any help would be greatly appreciated (See reply post below for error message)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#include "Expression.h"
#include "stackType.h"
using namespace std;
int main()
{
string fileName;
string infixExpression, postfixExpression;
Expression expression;
cout << "Converting infix expression to postfix expression\n"
<< "testing infix expressions are in input file\n" ;
cout << "Enter the input file name " ;
cin >> fileName;
ifstream inData;
ofstream outData;
inData.open(fileName.c_str());
outData.open("out.txt" );
cout<<"open=" <<fileName<<endl; //debugging print
while (inData>>infixExpression)
{
cout<<"Infix=" <<infixExpression<<endl; //debugging print
expression.setInfix(infixExpression);
cout<<"setInfix=" <<infixExpression<<endl;//debugging print
expression.convertInfixToPostfix();
cout<<"convert=" <<infixExpression<<endl; //debugging print
postfixExpression = expression.getPostfix();
cout << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n" ;
outData << "Infix Expression: "
<< infixExpression <<"\n"
<< "Postfix Expression: "
<<postfixExpression<<"\n\n" ;
}
inData.close();
outData.close();
system("pause" );
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#ifndef EXPRESSION_H_
#define EXPRESSION_H_
#include "stackType.h"
#include <stack>
#include <string>
#include <sstream>
#include "stackType.h"
#include <iostream>
using namespace std;
class Expression
{
private :
std::string infixExpression;
std::string postfixExpression;
bool precedence(stackType<char > s, char operator2); //ToDo
bool isOperand(char symbol); //ToDo
public :
Expression();
void setInfix(std::string infix);
std::string getInfix();
std::string getPostfix();
void convertInfixToPostfix(); //ToDo
static void convertInfixToPostfix( //ToDo
std::string& infix,
std::string& postfix);
virtual ~Expression();
};
#endif /* EXPRESSION_H_ */
=======================================================================
#include "Expression.h"
Expression::Expression() : infixExpression("" ), postfixExpression("" ){}
void Expression::setInfix(string infix)
{
infixExpression= infix;
}
string Expression::getInfix()
{
return infixExpression;
}
string Expression::getPostfix()
{
return postfixExpression;
}
void Expression::convertInfixToPostfix()
{
/*Define operator stack*/
int size = infixExpression.size();
stackType<char > s(size);
s.initializeStack();
ostringstream strstream;
int operand = 0;
int iter = 0;
for (; iter < size; ++iter)
{
switch (infixExpression[iter])
{
case '+' :
case '-' :
case '/' :
case '*' :
{
if (s.empty())
{
/*We always push the first operator*/
s.push(infixExpression[iter]);
}
else
{
if (precedence(s, infixExpression[iter]))
{
s.push(infixExpression[iter]);
}
else
{
do
{
strstream << s.top() << ' ' ;
s.pop();
}while (!s.empty() && !precedence(s, infixExpression[iter]));
/*Now push the current operator to the stack*/
s.push(infixExpression[iter]);
}
}
break ;
}
case ' ' :
{
break ;
}
default :
{
while (iter < size && infixExpression[iter] >= '0' && infixExpression[iter] <= '9' )
{
operand = operand * 10 + (infixExpression[iter]-'0' );
++iter;
}
strstream << operand << ' ' ;
operand = 0;
--iter;
break ;
}
}
}
while (!s.empty())
{
strstream << s.top() << ' ' ;
s.pop();
}
postfixExpression = strstream.str();
}
void Expression::convertInfixToPostfix(string& infix, string& postfix)
{
//ToDo
}
bool Expression::precedence(stackType<char > s, char operator_specified)
{
if ((operator_specified == '+' ) || (operator_specified == '-' ))
return false ;
char top_operator = s.top();
if ((top_operator == '+' ) || (top_operator == '-' ))
return true ;
else
return false ;
}
Expression::~Expression()
{
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
#ifndef STACKTYPE_H_
#define STACKTYPE_H_
#include <iostream>
#include <cassert>
#include "stackADT.h"
using namespace std;
template <class T>
class stackType : public stackADT<T>
{
public :
stackType(int stackSize = 100);
~stackType();
T top() const ;
void initializeStack();
bool empty() const ;
void push(const T& newItem);
bool isFullStack() const ;
void pop();
const stackType<T>& operator =(const stackType<T>&);
private :
int maxStackSize;
int stackTop;
char *list;
void copyStack(const stackType<T>& otherStack);
};
#endif /* stackType_H_ */
==============================================================================
#include "stackType.h"
template <class T>
stackType<T>::stackType(int stackSize)
{
if (stackSize <= 0)
{
cout << "Size of the array to hold the stack must be positive" << endl;
cout << "Creating an array of size 100" << endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new T[maxStackSize];
}
template <class T>
stackType<T>::~stackType()
{
delete [] list;
}
template <class T>
T stackType<T>::top() const
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template <class T>
void stackType<T>::initializeStack()
{
stackTop = 0;
}
template <class T>
bool stackType<T>::empty() const
{
return (stackTop == 0);
}
template <class T>
bool stackType<T>::isFullStack() const
{
return (stackTop == maxStackSize);
}
template <class T>
void stackType<T>::pop()
{
if (!empty())
stackTop--;
else
cout << "Cannot remove from an empty stack" << endl;
}
template <class T>
void stackType<T>::push(const T& newItem)
{
if (!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout << "Cannot add to a full stack" << endl;
}
template <class T>
void stackType<T>::copyStack(const stackType<T>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new T[maxStackSize];
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template <class T>
const stackType<T>& stackType<T>::operator =(const stackType& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);
return *this ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef STACKADT
#define STACKADT
#include <iostream>
using namespace std;
template <class T>
class stackADT
{
public :
virtual void initializeStack() = 0;
virtual bool empty() const = 0;
virtual bool isFullStack() const = 0;
virtual void push(const T& newItem) = 0;
virtual T top() const = 0;
virtual void pop() = 0;
};
#endif
Last edited on Mar 27, 2014 at 12:11am UTC
Mar 27, 2014 at 12:08am UTC
The repeated error message I get is:
1>Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType<char>::stackType<char>(int)" (??0?$stackType@D@@QAE@H@Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: __thiscall stackType<char>::~stackType<char>(void)" (??1?$stackType@D@@QAE@XZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: virtual char __thiscall stackType<char>::top(void)const " (?top@?$stackType@D@@UBEDXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall stackType<char>::initializeStack(void)" (?initializeStack@?$stackType@D@@UAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall stackType<char>::empty(void)const " (?empty@?$stackType@D@@UBE_NXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall stackType<char>::push(char const &)" (?push@?$stackType@D@@UAEXABD@Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>Expression.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall stackType<char>::isFullStack(void)const " (?isFullStack@?$stackType@D@@UBE_NXZ)
1>Expression.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall stackType<char>::pop(void)" (?pop@?$stackType@D@@UAEXXZ) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)
1>C:\Users\Jake Bock\Desktop\CSC 240\Project 7\JBock_Project7\Debug\JBock_Project7.exe : fatal error LNK1120: 8 unresolved externals
Mar 27, 2014 at 1:05am UTC
Make sure all the .cpp files are added to your project tree in Visual Studio. If they are not in the project tree they will not be compiled.