Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

CompSci: Undefined reference to

  1. Mar 17, 2009 #1
    1. The problem statement, all variables and given/known data

    I'm compiling ExprTree.cpp and it's giving me an Undefined reference to 'BinaryTree::BinaryTree(std::string)', 'BinaryTree::toString()', 'BinaryTree::~BinaryTree()', and an undefined reference to '_WinMain@16'
    ExprTree inherits from BinaryTree

    2. Relevant equations

    This is my code:


    BinaryTree.cpp
    Code (Text):

    /* BinaryTree.cpp
     *
     */

    #include <iostream>
    #include "BinaryTree.h"

    BinaryTree::BinaryTree() {
        left = 0;
        right = 0;
        this->key = "Default Key";
    }

    BinaryTree::BinaryTree(string key)
    {
        left = 0;
        right = 0;
        this->key = key;
    }

    BinaryTree::~BinaryTree() {

        if(left != 0)
            delete left;
        if(right != 0)
            delete right;
    }

    string BinaryTree::getKey()
    {
        return key;
    }

    BinaryTree* BinaryTree::leftChild()
    {
        return left;
    }

    BinaryTree* BinaryTree::rightChild()
    {
        return right;
    }

    void BinaryTree::setLeftChild(BinaryTree* left)
    {
        this->left = left;
    }

    void BinaryTree::setRightChild(BinaryTree* right)
    {
        this->right = right;
    }

    string BinaryTree::toString()
    {
        string stringRep = "[" + getKey();

        if(this->left && this->right)
            stringRep += ", " + this->left->toString() + ", " + this->right->toString();
        else if(this->left)
            stringRep += ", " + this->left->toString() + ", []";
        else if(this->right)
            stringRep += ", [], " + this->right->toString();

        return  stringRep + "]";
    }


     

    BinaryTree.h
    Code (Text):

    /* BinaryTree.h
     *
     */

    #ifndef BINARYTREE_H_
    #define BINARYTREE_H_
    #include <string>

    using namespace std;

    class BinaryTree {
        public:
            BinaryTree();
            BinaryTree(string);
            virtual ~BinaryTree();

            BinaryTree* leftChild();
            BinaryTree* rightChild();
            string getKey();

            virtual string toString();

            void setLeftChild(BinaryTree*);
            void setRightChild(BinaryTree*);

        protected:
            BinaryTree *left;
            BinaryTree *right;

            string key;
    };

    #endif /* BINARYTREE_H_ */

     
    ExprTree.h
    Code (Text):

    /* ExprTree.h
     *
     */

    #ifndef EXPRTREE_H_
    #define EXPRTREE_H_
    #include "BinaryTree.h"

    class ExprTree:public BinaryTree
    {
        public:
            ExprTree(string key);
            virtual ~ExprTree()
            {
                if(left != 0)
                    delete left;
                if(right != 0)
                    delete right;
            }
            virtual double evaluate() = 0;
    };

    class OperatorTree:public ExprTree
    {
        public:
            OperatorTree(string);
            virtual ~OperatorTree();

            virtual double evaluate();
    };

    class ValueTree:public ExprTree
    {
        public:
            ValueTree(string, int);
            virtual ~ValueTree();

            virtual double evaluate();

        protected:
            int value;
    };

    #endif /* EXPRTREE_H_ */

     

    ExprTree.cpp
    Code (Text):

    /* ExprTree.cpp
     *
     */

    #include <iostream>
    #include "ExprTree.h"
    #include "BinaryTree.h"

    ExprTree::ExprTree(string key): BinaryTree(key)
    {
    }

     
    Code (Text):

    int main()
    {

        string key = "hello";
        BinaryTree *ptr = new BinaryTree(key);
        delete ptr;
        cout << "BinaryTree b" << endl;

    }
     
    3. The attempt at a solution

    Can anyone also give me hints as to how to make an expression tree? i'm having lots of trouble. Do I need to use a stack? What do I do with the OperatorTree's children if they are not constants but instead operators? I don't know how to use the getkey function from the BinaryTree since Operator isn't directly it's child class.
     
    Last edited: Mar 17, 2009
  2. jcsd
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?