-3

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;

}
Tommy Boi
  • 37
  • 1
  • 7
  • You stated _The only thing is that It's not really telling me what the problem is._ but that's invalid. You pasted in the exact error: `(.text+0x20): undefined reference to 'main'` – mah Apr 02 '14 at 10:18
  • -1 This could have easily been resolved by a simple web search for your error message. – lethal-guitar Apr 02 '14 at 10:25
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Apr 02 '14 at 10:43

2 Answers2

2

It means you are trying to compile an executable program that has no main function defined. The program doesn't know which function to run first.

To fix this you need to add int main(){ //body return 0; } somewhere in the global context in your source code.

dburner
  • 1,007
  • 7
  • 22
  • Thankyou. The problem is I didn't exactly know what the error message was, so I just tried searching for the entire message and didn't get any useful results. I figured it out though – Tommy Boi Apr 03 '14 at 02:37
  • Right on target. I searched so many answers and only this one gave me the solution. `main()` function was missing. – Nav Oct 28 '15 at 07:35
0

The following command: g++ -o bet.x bet.cpp says make an executable called bet.x from a single file: bet.cpp

however to make an executable you need a main() function which is missing.

If you just want to make sure the syntax is ok you can build an object file by passing -c to g++

Another option is to add a main() function maybe even write a few tests to check that the code works.

odedsh
  • 2,594
  • 17
  • 17