无法在终端中编译c ++项目

时间:2015-11-22 22:25:00

标签: c++ c++11 terminal

我的任务是创建一个二进制表达式树,将后缀表达式转换为C ++中的中缀表达式。我最初在我的Xcode IDE中编写了我的所有工作,并定期ssh到终端中的linprog4,以确保它在那里工作,因为它必须在我打开之前。我不明白当我使用它编译它时弹出的错误g ++ -o proj4.x -std = c ++ 11 proj4_driver.cpp BET.cpp

这项任务已于2周前发布,不幸的是,当我使用我的所有文件转入tar文件时,我忘了包含.tar扩展名。我不认为这会影响我的文件,但现在他们没有编译,我不明白我得到的错误。有人可以浏览我的代码,看看我是否遗漏了什么?我查看了我的代码,看起来并不像我偶然在某处随意输入了一些东西。

以下是我收到的错误的屏幕截图 enter image description here

BET.h

#ifndef BET_H
#define BET_H

using namespace std;

class BET {
    private:

        struct BinaryNode {

            string data;
            BinaryNode * parent;
            BinaryNode * childLeft;
            BinaryNode * childRight;
            bool visited;           // to tell if node has been visited to or not


            // Construct a blank copy version BinaryNode when an
            // object of BET is created
            BinaryNode( const char & d = char{}, BinaryNode * p = NULL,
                        BinaryNode * l = NULL,
                        BinaryNode * r = NULL,  bool v = false )
                        : data { d },
                        parent { p },
                        childLeft { l },
                        childRight { r },
                        visited { v } {}

            // Construct a blank move version of BinaryNode when an
            // an object of BET is created
            BinaryNode( char && d, BinaryNode * p = NULL, BinaryNode * l = NULL,
                        BinaryNode * r = NULL, bool v = false )
                        : data { std::move(d) },
                        parent { p },
                        childLeft { l },
                        childRight { r },
                        visited { v } {}

        };  // end of BinaryNode struct

    public:

        // constructors and destructor
        BET();
        BET( const string postfix );
        BET( const BET & rhs );
        ~BET();

        // help copy constructor
        bool buildFromPostfix( const string postfix );

        // copy assignment operator
        const BET & operator=( const BET & rhs );

        void printInfixExpression();
        void printPostfixExpression();

        size_t size();
        size_t leaf_nodes();

        bool empty();
        bool isOperand(BinaryNode * n);

    private:
        BinaryNode *root;
        size_t leaves, nodes;
        bool useP;

        void printInfixExpression( BinaryNode * n );
        void makeEmpty( BinaryNode* & t );
        BinaryNode * clone( BinaryNode * t ) const;
        void printPostfixExpression( BinaryNode * n );
        size_t size( BinaryNode * t );
        size_t leaf_nodes( BinaryNode * t );


}; // end of BET class

#endif

BET.cpp

#include <iostream>
#include <stack>
#include <sstream>
#include <string>
#include "BET.h"

//using namespace std;

// default zero-param constructor
BET::BET() {

    root = new BinaryNode;
    leaves = 0;
    nodes = 0;
}

// one-param constructor, where parameter "postfix" is a
// string containing a postfix expression. The tree should
// be built based on the postfix expression. Tokens in the
// postfix expression are separated by space
BET::BET(const string postfix) {

    root = new BinaryNode;
    buildFromPostfix(postfix);
}

// copy constructor
BET::BET(const BET & rhs) {

    leaves = rhs.leaves;
    nodes = rhs.nodes;
    root = rhs.root;
}

// destructor
BET::~BET() {

    makeEmpty(root);
    leaves = nodes = 0;
}

bool BET::buildFromPostfix(const string postfix) {

    // Create stack to hold variables
    stack<BinaryNode *> s;
    stack<BinaryNode> bet;
    char token;
    string temp;
    int index = 1;
    bool doubleDigit = false;
    int opCount = 0, digitCount = 0;

    //stringstream hexToInt;

    // iterator through postfix
    for (int i = 0; i < postfix.size(); ++i) {

        // grab token at iterations index
        token = postfix[i];


        if ( (token > '0' && token < '9') || (token > 62 && token < 80)) {
           // check to see if token is an operand

            // create a dynamic object of BinaryNode
            BinaryNode *operand = new BinaryNode;

            // check to see if next index of postfix is digit
            // if its not, then we know its a double digit
            // this while loop should only continue as long as the
            // next index is between 0 and 9

            temp = postfix[i];
            while (postfix[i + index] >= '0' && postfix[i + index] <= '9') {

                temp += postfix[i + index];
                index++;
                doubleDigit = true;
            }

            if (doubleDigit == true) {

                i += index;
                doubleDigit = false;
                index = 1;
                operand->data = temp;

            } else {

                operand->data = postfix[i];
            }

            s.push(operand);
            digitCount++;

        } else if (token == '+' || token == '-' || token == '*' || token == '/'){
            // check to see if token is operator

            BinaryNode *operand = new BinaryNode;
            operand->data = postfix[i];
            operand->childLeft = s.top();
            s.top()->parent = operand;
            s.pop();
            operand->childRight = s.top();
            s.top()->parent = operand;
            s.pop();
            s.push(operand);
            opCount++;
        } else {
            // if neither, must be space or other character

            if (token == ' ') {

            } else
                return false;
        }

    }

    if (digitCount <= opCount) {
        return false;
    }

    root = s.top();

    nodes = size();
    //leaves = leaf_nodes();

    // THINGS TO DO:
    // Make error cases with if statements to return false at some point
    return true;
}

// assignment operator
const BET & BET::operator=(const BET & rhs) {

    root = clone(rhs.root);

    return *this;
}

// public version of printInfixExpression()
// calls the private version of the printInfixExpression fuction
// to print out the infix expression
void BET::printInfixExpression() {


    printInfixExpression(root);
}

// public version of printPostfixExpression()
// calls the private version of the printPostfixExpression function
// to print out the postfix expression
void BET::printPostfixExpression() {

    printPostfixExpression(root);
}

// public version of size()
// calls the private version of the size function to return
// the number of nodes in the tree
size_t BET::size() {

    return size(root);
}


// public version of leaf_nodes()
// calls the private version of leaf_nodes function to return
// the number of leaf nodes in the tree
size_t BET::leaf_nodes() {

    return leaf_nodes(root);
}

// public version of empty()
// return true if the tree is empty. return false otherwise
bool BET::empty() {

    if (nodes == 0) {
        return true;
    } else
        return false;
}

// checks whether node is operand or not
bool BET::isOperand(BinaryNode * n) {

    if (n->data != "+" && n->data != "-" && n->data != "*" && n->data != "/") {
        return true;
    } else
        return false;

}
// private version of printInfixExpression
// print to the standard output the corresponding infix expression
void BET::printInfixExpression(BinaryNode * n) {
    //BinaryNode * temp = NULL;

    if (n != NULL ) {
        printInfixExpression(n->childRight);
        cout << n->data << " ";
        printInfixExpression(n->childLeft);
    }

    /*
    if (n != NULL && useP == true) {

        printInfixExpression(n->childRight);

        if (isOperand(n->parent)  && n->parent != NULL && !n->childLeft) {
            cout << "(";

        }

        cout << n->data << " ";

        if (isOperand(n->parent) && n->parent != NULL && !n->childRight) {
            cout << ")";
        }

        printInfixExpression(n->childLeft);

    }
    */
}

// private method makeEmpty()
// delete all nodes in the subtree pointed to by n.
// Called by functions such as the destructor
void BET::makeEmpty(BinaryNode * & n) {

    if (n != NULL) {
        makeEmpty(n->childLeft);
        makeEmpty(n->childRight);
        delete n;
    }
}

// private method clone()
// clone all nodes in the subtree pointed by n. Called by
// functions such as the assignment operator=
BET::BinaryNode * BET::clone(BinaryNode * n) const {


    if (n != NULL) {
        root->childRight = clone(n->childRight);
        root->childLeft = clone(n->childLeft);
        root->data = n->data;
    }

    return root;
}

// private method printPostfixExpression()
// print to the standard output the corresponding postfix expression
void BET::printPostfixExpression(BinaryNode * n) {

    if (n != NULL) {
        printPostfixExpression(n->childRight);
        printPostfixExpression(n->childLeft);
        cout << n->data << " ";
    }
}

// private version of size()
// return the number of nodes in the subtree pointed by n
size_t BET::size(BinaryNode * n) {


    if (n != NULL) {
        size(n->childLeft);
        size(n->childRight);
        nodes++;
    }

    return nodes;
}

// return the number of leaf nodes in the subtree pointed by n
size_t BET::leaf_nodes(BinaryNode * n) {

    if (n != NULL) {
        leaf_nodes(n->childLeft);
        leaf_nodes(n->childRight);

        if (n->childLeft == NULL && n->childRight == NULL) {
            leaves += 1;
        }
    }

    return leaves;
}

Driver.cpp

#include "BET.cpp"

//using namespace std;


int main() {
    string postfix;

    // get a postfix expression
    cout << "Enter the first postfix expression: ";
    getline(cin, postfix);

    // create a binary expression tree
    BET bet1(postfix);

    if (!bet1.empty()) {
        cout << "Infix expression: ";
        bet1.printInfixExpression();
        cout << "\n";

    cout << "Postfix expression: ";
    bet1.printPostfixExpression();

    cout << "\nNumber of nodes: ";
    cout << bet1.size() << endl;

    cout << "Number of leaf nodes: ";
    cout << bet1.leaf_nodes() << endl;

        // test copy constructor
        BET bet2(bet1);
        cout << "Testing copy constructor: ";
        //bet2.printInfixExpression();

        // test assignment operator
        BET bet3;
        bet3 = bet1;
        cout << "Testing assignment operator: ";
        //bet3.printInfixExpression();
    }

    cout << "Enter a postfix expression (or \"quit\" to quit): ";
    while (getline(cin, postfix)) {
    if (postfix == "quit") {
        break;
        }
    if (bet1.buildFromPostfix(postfix)) {
        cout << "Infix expression: ";
        bet1.printInfixExpression();

        cout << "Postfix expression: ";
        bet1.printPostfixExpression();

        cout << "Number of nodes: ";
        cout << bet1.size() << endl;

        cout << "Number of leaf nodes: ";
        cout << bet1.leaf_nodes() << endl;
    }
    cout << "Enter a postfix expression (or \"quit\" to quit): ";
    }
    return 0;

}

1 个答案:

答案 0 :(得分:2)

Driver.cpp #include "BET.cpp"应为#include "BET.h"

或者(这只是为了完整性,不推荐),保留.cpp的包含,但只尝试编译一个.cpp(如g++ Driver.cpp中所述) - 因为驱动程序将包含BET和所以你的所有代码都在那里并将构建。