codeblocks c ++错误:未定义引用'subclass :: subclass()'

时间:2016-04-24 21:03:50

标签: c++ c++11

我正在为学校开发一个c ++项目,它是一个计算器程序,它接受一个中缀表达式并将其转换为后缀,然后简化表达式,避免浮点数。当我创建一个指向某个子类对象的超类(Expression)的指针(例如Addition)时,我遇到的问题是main,它无法找到构造函数。请让我知道我做错了什么。这是我第一次发帖。谢谢!

(编辑) 我正在使用codeblocks项目。它似乎不是链接器错误。

main.cpp中:

    #include "InfixToPostfix.h"
    #include "Expression.h"
    #include "Addition.h"
    #include "Subtraction.h"
    #include "Multiplication.h"
    #include "Division.h"
    #include "Exponentiation.h"
    #include "NthRoot.h"
    #include "Logarithm.h"
    #include "Integer.h"

    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <limits>
    #include <string>
    #include <vector>
    #include <typeinfo>

    using namespace std;

    Expression* createExp(char a){

        if(a == '+'){
            Addition addObj;    //////////// This is where the error is, 
            Expression* addExp = &addObj; // I have tried a few ways...
    //        addObj = new Addition;
            return addExp;
        }else if(a == '-'){
            Subtraction* subObj;
    //        subObj = new Subtraction;
            return subObj;
        }else if(a == '*'){
           Multiplication* mulObj;
      //      mulObj = new Multiplication;
           return mulObj;
      }else if(a == '/'){
            Multiplication* divObj;
    //        divObj = new Division;
            return divObj;
        }else if(a == '^'){
            Exponentiation* expObj;
    //        expObj = new Exponentiation;
            return expObj;
    //    }else if(a == "rt"){
    //        return new NthRoot();
    //    }else if(a == "log"){
    //        return new Logarithm();
        }else{
            Integer* intObj;
    //        intObj = new Integer;
            int b = a-48; // convert to int
            intObj->setValue(b);
            return intObj;
        }
    }

    int main()
    {
        InfixToPostfix object;
        string postFix = object.convert();
        cout << postFix;

        Expression* root = createExp(postFix[postFix.length() - 1]); 
        Expression* currentNode = root;
        Expression* nextNode;

        int c = postFix.length() - 2; // second from last
        while(c >= 0)
        {
            if(postFix[c] == ' '){
                c--;

            }else{
                nextNode = createExp(postFix[c]);
                if(typeid(currentNode) == typeid(Integer)){

                }
                if(currentNode->getRightSide() != 0)
                {
                    currentNode->rightSide = nextNode;
                    currentNode = nextNode;

                }else if(currentNode->getLeftSide() != 0)
                {
                    currentNode->leftSide = nextNode;
                    currentNode = nextNode;
                }

            } 

             c--;
        }
        cout << root->getValue();
        return 0;
    }

Expression.h:

    #ifndef EXPRESSION_H_INCLUDED
    #define EXPRESSION_H_INCLUDED

    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <limits>
    #include <string>
    #include <vector>

    using namespace std;

    class Expression
    {
    private:
    protected:  
        int value;
    public:
        Expression();
        virtual ~Expression()=0;
        virtual void setValue(int b)=0;
        virtual int getValue()=0;
        Expression* leftSide;
        Expression* rightSide;

        virtual Expression* getLeftSide()=0;
        virtual Expression* getRightSide()=0;

        virtual vector<Expression*> getNumeratorFactors()=0;

        virtual vector<Expression*> getDenominatorFactors()=0;

        virtual vector<Expression*> getAdditiveTerms()=0;

        virtual Expression* simplify()=0;

    };
    #endif // EXPRESSION_H_INCLUDED

Expression.cpp:

    #include "Expression.h"

    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <limits>
    #include <string>
    #include <vector>
    using namespace std;

    Expression :: Expression()
    {
        LeftSide = 0;
        RightSide = 0;


    }

    Expression :: ~Expression()
    {


    }

Addition.h:

    #ifndef ADDITION_H_INCLUDED
    #define ADDITION_H_INCLUDED
    #include "Expression.h"

    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <limits>
    #include <string>
    #include <vector>
    using namespace std;

    class Addition: public Expression
    {
    private:
        int value;
    public:
        Addition();
        ~Addition();
        virtual void setValue(int b);
        virtual int getValue();
        Expression* leftSide;
        Expression* rightSide;
        virtual Expression* getLeftSide();
        virtual Expression* getRightSide();
        virtual vector<Expression*> getNumeratorFactors();
        virtual vector<Expression*> getDenominatorFactors();
        virtual vector<Expression*> getAdditiveTerms();
        virtual Expression* simplify();
    };


    #endif // ADDITION_H_INCLUDED

Addition.cpp:

    #include "Addition.h"

    #include <iostream>
    #include <sstream>
    #include <stack>
    #include <limits>
    #include <string>
    #include <vector>

    using namespace std;

    Addition::Addition()
    {
        leftSide = 0;
        rightSide = 0;

        setValue(int b)=0;
        getValue()=0;

        getLeftSide()=0;
        getRightSide()=0;
        getNumeratorFactors()=0;
        getDenominatorFactors()=0;
        getAdditiveTerms()=0;
        simplify()=0;

    }
    Addition :: ~Addition()
    {

    }

    virtual int Addition :: getValue()
    {
        return getLeftSide().getValue() + getRightSide().getValue();
    }

    virtual Expression* Addtion :: getLeftSide()
    {
        return leftSide;
    }

    virtual Expression* Addtion :: getRightSide()
    {
        return rightSide;
    }


    virtual vector<Expression*> Additon :: getNumeratorFactor()
    {


    }

    virtual vector<Expression*> Additon :: getDenominatorFactor()
    {


    }

    virtual vector<Expression*> Additon :: getAdditiveTerms()
    {


    }

    virtual Expression* Additon :: simplify()
    {
        return getLeftSide() + getRightSide();

    }

0 个答案:

没有答案