- #1
subwaybusker
- 51
- 0
Homework Statement
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
Homework Equations
This is my code:
BinaryTree.cpp
Code:
/* 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:
/* 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:
/* 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:
/* ExprTree.cpp
*
*/
#include <iostream>
#include "ExprTree.h"
#include "BinaryTree.h"
ExprTree::ExprTree(string key): BinaryTree(key)
{
}
Code:
int main()
{
string key = "hello";
BinaryTree *ptr = new BinaryTree(key);
delete ptr;
cout << "BinaryTree b" << endl;
}
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: