So in this project I have to develop a binary expression tree and use the tree to convert postfix expressions into infix expressions. Everythings looking ok for the most part, (besides one or two of the functions that I'm yet to complete). But I'm having an error when trying to compile it. The only thing is that It's not really telling me what the problem is. Here's the problem I get when I try to compile it:
g++ -o bet.x bet.cpp /usr/lib/gcc/x86_64-redhat-linux6E/4.4.7/../../../../lib64/crt1.o: In function `_start': (.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [bet.x] Error 1
I have no idea what this means. But just incase here's my 3 files. Starting with my makefile:
bet.x: bet.cpp
g++ -o bet.x bet.cpp
clean:
rm -f *.o *.x core.*
heres my bet.h (header file) that holds my interfaces:
#include <string>
using namespace std;
struct BinaryNode
{
string element;
BinaryNode* leftNode;
BinaryNode* rightNode;
};
class BET
{
public:
BET();
BET(const string postfix);
BET(const BET&);
~BET();
bool buildFromPostfix(const string postfix);
const BET& operator= (const BET&);
void printInfixExpression();
void printPostfixExpression();
size_t size();
size_t leaf_nodes();
bool empty();
private:
void printInfixExpression(BinaryNode *n);
void makeEmpty(BinaryNode* &t);
BinaryNode* clone(BinaryNode* t) const;
BinaryNode* headNode;
void printPostfixExpression(BinaryNode *n);
size_t size(BinaryNode *t);
size_t leaf_nodes(BinaryNode *t);
};
It'd probably be too much to show you my whole bet.cpp file that has all my implementations. But here's some of it. Comment if more is needed:
BET::BET()
{
headNode = NULL;
}
BET::BET(const string postfix)
{
headNode = NULL;
buildFromPostfix(postfix);
}
BET::BET(const BET& t)
{
headNode = clone(t.headNode);
}
BET::~BET()
{
makeEmpty(headNode);
}
//2 functions i made myself for high/low precedence
bool isHighPrec(const string& op)
{
return op == "*" || op == "/";
}
bool isLowPrec(const string& op)
{
return op == "+" || op == "-";
}
//returns false for errors, returns true otherwise
bool BET::buildFromPostfix(const string postfix){
stack<BinaryNode*> opTree;
istringstream istr(postfix);
string op;
makeEmpty(headNode);
while(istr >> op)
{
if(isHighPrec(op) || isLowPrec(op))
{
if(opTree.size() < 2)
{
cout << "Error -- invalid postfix expression!\n";
while(opTree.size())
{
makeEmpty(opTree.top());
opTree.pop();
}
return false;
}
else
{
BinaryNode* bn = new BinaryNode();
bn->element = op;
bn->rightNode = opTree.top();
opTree.pop();
bn->leftNode = opTree.top();
opTree.pop();
opTree.push(bn);
}
}
else
{
BinaryNode* bn = new BinaryNode();
bn->element = op;
bn->rightNode = NULL;
bn->leftNode = NULL;
opTree.push(bn);
}
}
if(opTree.size() != 1)
{
cout << "Error -- invalid postfix expression!\n";
while(opTree.size())
{
makeEmpty(opTree.top());
opTree.pop();
}
return false;
}
else
{
headNode = opTree.top();
opTree.pop();
}
return true;
}