麻烦运行中缀到postfix堆栈转换程序

时间:2014-03-26 18:28:28

标签: c++ postfix-notation

我每次都在Visual Studio上收到相同的错误消息。我不知道错误的起源。基本上我试图使用堆栈将中缀表达式(A + B-C)转换为后缀表达式(AB + C-)。任何帮助将不胜感激

  

DriverExpression.cpp

     

Expression.obj:错误LNK2019:未解析的外部符号" public:__ thiscall stackType :: stackType(int)"函数" public:void __thiscall Expression :: convertInfixToPostfix(void)"(?? 0?$ stackType @ D @@ QAE @ H @ Z) (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:__ thishisall stackType :: ~stragageType(void)"函数" public:void __thiscall Expression :: convertInfixToPostfix(void)"(?? 1?$ stackType @ D @@ QAE @ XZ) (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:char __thiscall stackType :: top(void)const"函数" public:void __thiscall Expression :: convertInfixToPostfix(void)"(?top @?$ stackType @ D @@ QBEDXZ) (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:void __thiscall stackType :: initializeStack(void)"函数" public中引用了(?initializeStack @?$ stackType @ D @@ QAEXXZ):void __thiscall Expression :: convertInfixToPostfix(void)" (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:bool __thiscall stackType :: empty(void)const" (?empty @?$ stackType @ D @@ QBE_NXZ)在函数&#34中公开; public:void __thiscall Expression :: convertInfixToPostfix(void)" (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:void __thiscall stackType :: push(char)" (?push @?$ stackType @ D @@ QAEXD @ Z)在函数" public中引用:void __thiscall Expression :: convertInfixToPostfix(void)" (?convertInfixToPostfix @ @@表达QAEXXZ)

     

Expression.obj:错误LNK2019:未解析的外部符号" public:void __thiscall stackType :: pop(void)" (?pop @?$ stackType @ D @@ QAEXXZ)函数" public:void __thiscall Expression :: convertInfixToPostfix(void)" (?convertInfixToPostfix @ @@表达QAEXXZ)

     

C:\ Users \ Jake Bock \ Desktop \ CSC 240 \ Project 7 \ JBock_Project7 \ Debug \ JBock_Project7.exe:致命错误LNK1120:7个未解析的外部**

#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;
}

=============================================== =================

    #ifndef EXPRESSION_H_
#define EXPRESSION_H_
#include "stackType.h"

class Expression
{
private:
    std::string infixExpression;
    std::string postfixExpression;

    bool precedence(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 <stack>
#include <string>
#include <sstream>
#include "Expression.h"
#include "stackType.h"
#include <iostream>
using namespace std;


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*/
    stackType<char> s(infixExpression.size());
    s.initializeStack();
    ostringstream strstream;

    int operand = 0;
    /*String stream will help us pushing integer to our stream easily.
    This is similar to sprintf(), but with less hassles.*/
    int size = infixExpression.size();
    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 of current operator is higher than
                    one on top of the stack, we simply push the current
                    operator to the top of the stack*/
                    if (precedence(s.top(), infixExpression[iter]))
                    {
                        s.push(infixExpression[iter]);
                    }
                    else
                    {
                        /*Pop from the stack and append it to string stream.
                        Repeat it unless we find an operator on top of the stack
                        whose precedence is lower or stack is empty.*/
                        do
                        {
                            strstream << s.top() << ' ';
                            s.pop();
                        }while (!s.empty() && !precedence(s.top(), 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;
            }
        }
    }
    /*Pop one by one from operator stack and append it to our stream*/
    while(!s.empty())
    {
        strstream << s.top() << ' ';
        s.pop();
    }
    /*Get the string associated with our string stream*/
    postfixExpression = strstream.str();
}


bool Expression::precedence(char s, char operator_specified)
{

    if ((operator_specified == '+') || (operator_specified == '-'))
        return false;
    char top_operator = s;
    if ((top_operator == '+') || (top_operator == '-'))
        return true;
    else
        return false;
}

Expression::~Expression()
{
}

// ============================================= ==========================================

 #ifndef STACKTYPE_H_
#define STACKTYPE_H_


template <class T>
class stackType
{
private:

    int maxStackSize;
    int stackTop;
    char *list;

    void copyStack(const stackType& otherStack);

public:

    stackType();
    stackType(int stackSize);
    ~stackType();
    char top() const;
    void initializeStack();
    bool empty() const;
    void push(const char newItem);
    bool isFullStack() const;
    void pop();
    const stackType& operator=(const stackType& otherStack);
};

#endif /* stackType_H_ */

#include "stackType.h"
#include <stack>
#include <cassert>

template <class T>
stackType<T>::stackType(){}

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 char[maxStackSize];
}

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

template <class T>
char 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 (!isEmptyStack())
        stackTop--;
    else
        cout << "Cannot remove from an empty stack" << endl;
}

template <class T>
void stackType<T>::push(const char 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& otherStack)
{
    delete [] list;
    maxStackSize = otherStack.maxStackSize;
    stackTop = otherStack.stackTop;
}


template <class T>
const stackType<T>& stackType<T>::operator=(const stackType& otherStack)
{
    if (this != &otherStack)
        copyStack(otherStack);
    return *this;
}

2 个答案:

答案 0 :(得分:0)

unresolved external symbol

好的,所以你在这里遇到了链接器错误。每当你得到它,这是解决它的过程。首先,让我们看一下错误信息。

public: __thiscall stackType::stackType(int)" (??0?$stackType@D@@QAE@H@Z) referenced in function "public: void __thiscall Expression::convertInfixToPostfix(void)" (?convertInfixToPostfix@Expression@@QAEXXZ)

  1. 找出什么方法没有链接。查看错误消息,它显然是 stackType :: stackType(int)
  2. 现在发生的是你的方法(在这种情况下,构造函数)被声明但从未定义过。所以,仔细阅读清单......

    1. stackType.cpp是否包含在您的构建中?

    2. 您还记得在.cpp文件中编写stackType :: stackType(int)吗?

    3. 这两个函数的方法签名是否相同?你有一个const方法而另一个没有吗?

    4. 类或方法名称中的任何奇怪的拼写错误?

    5. 您是否在做涉及模板的错误?

    6. 我的猜测是#4是罪魁祸首,因为stackType是一个模板类。您需要将所有这些方法定义都粘贴到标题中,并且可能不是。

答案 1 :(得分:0)

所以问题出来了我只需要在头文件中保留模板类成员函数定义。 Oy公司。