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

标签 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

下注.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

下注.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;
}

驱动.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;

}

最佳答案

Driver.cpp #include "BET.cpp" 应该是 #include "BET.h"

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

关于c++ - 无法在终端中编译 C++ 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860980/

相关文章:

c++ - int&& 和 template<class T> T&& 的区别

c++ - 在库中公开 constexpr 专用模板函数

由于与 mariadb 冲突,MySQL 无法在 CentOS 中安装

linux - 有什么方法可以获取搜索模式 PDF 中的页码吗?

c++ - 将 WIC 位图绘制到窗口?

c++ - 如何以非内联方式定义模板覆盖的功能

c++ - 迭代时从 vector 和内存中删除一个对象

c++ - 在 C++11 中结合 vector 和整数?

python - 在控制台上显示有向图

c++ - 如何在 VS 代码中为 Bazel 项目启用 C++ 智能感知?