2015-12-01 178 views
0

我试图学习评估表达式的二叉树的实现。我无法运行它并查看输出。我怎么会得到3 *(7 + 1)/ 4 +(17-5),这将导致18 这里是链接http://math.hws.edu/eck/cs225/s03/binary_trees/评估表达式二叉树C++

class ExpNode { 
      // Represents a node of any type in an expression tree. 
      // This is an "abstract" class, since it contains an undefined 
      // function, value(), that must be defined in subclasses. 
      // The word "virtual" says that the defintion can change 
      // in a subclass. The "= 0" says that this function has 
      // no definition in this class. 

    public:  

     virtual double value() = 0; // Return the value of this node. 

    }; // end class ExpNode 


class ConstNode : public ExpNode { 
      // Represents a node that holds a number. (The 
      // ": public ExpNode" says that this class is 
      // a subclass of ExpNode.) 

     double number; // The number in the node. 

    public: 

     ConstNode(double val) { 
      // Constructor. Create a node to hold val. 
      number = val; 
     } 

     double value() { 
      // The value is just the number that the node holds. 
      return number; 
     } 

    }; // end class ConstNode 


class BinOpNode : public ExpNode { 
      // Represents a node that holds an operator. 

     char op;  // The operator. 
     ExpNode *left; // The left operand. 
     ExpNode *right; // The right operand. 

    public: 

     BinOpNode(char op, ExpNode *left, ExpNode *right) { 
      // Constructor. Create a node to hold the given data. 
      this->op = op; 
      this->left = left; 
      this->right = right; 
     } 

     double value() { 
      // To get the value, compute the value of the left and 
      // right operands, and combine them with the operator. 
      double leftVal = left->value(); 
      double rightVal = right->value(); 
      switch (op) { 
       case '+': return leftVal + rightVal; 
       case '-': return leftVal - rightVal; 
       case '*': return leftVal * rightVal; 
       case '/': return leftVal/rightVal; 
      } 
     } 

    }; // end class BinOpNode 

这是我试图做一个主要功能:

int main() { 
    BinOpNode *opnode; 
    opnode = new BinOpNode; 
    opnode->value()=5; 
    ExpNode *expnode; 
    expnode = opnode; 
    expnode->value(); 
    return 0; 

} 

它不能编译,这是错误

15:58:27 **** Incremental Build of configuration Debug for project ExpNode **** 
Info: Internal Builder is used for build 
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\ExpNode.o" "..\\src\\ExpNode.cpp" 
..\src\ExpNode.cpp: In function 'int main()': 
..\src\ExpNode.cpp:60:15: error: no matching function for call to 'BinOpNode::BinOpNode()' 
..\src\ExpNode.cpp:36:2: note: candidates are: BinOpNode::BinOpNode(char, ExpNode*, ExpNode*) 
..\src\ExpNode.cpp:30:33: note:     BinOpNode::BinOpNode(const BinOpNode&) 
..\src\ExpNode.cpp:61:18: error: lvalue required as left operand of assignment 

15:58:28 Build Finished (took 405ms) 
+0

你怎么*无法运行它,看到输出*?它没有编译?如果没有,你应该发布编译器错误。 – NathanOliver

+0

我刚编辑! – sm15

+1

@ sm15您正在构造一个没有参数的'BinOpNode'对象:'opnode = new BinOpNode;'。看看你的'BinOpNode'类。你看到没有这样的构造函数没有任何参数。这正是错误告诉你的。 – PaulMcKenzie

回答

0

在C++中,默认的构造函数有趣地工作。

如果不定义任何构造,为您生成一个默认的构造函数:

class A {}; 

int main() 
{ 
    A a; // perfectly fine 

但是,如果你定义任何其他构造,这些构造产生走开:

class A 
{ 
    A(int) { ... } 
}; 

int main() 
{ 
    A a; // ERROR! 
} 

在在这种情况下,缺省构造函数不存在是因为您定义了一个构造函数,编译器也没有为您生成。

这是你的问题,因为在这里main,你有这样一行:

opnode = new BinOpNode; 

它运行的BinOpNode默认构造函数。看看你BinOpNode构造函数:

BinOpNode(char op, ExpNode *left, ExpNode *right) 

嘿看:那不是一个默认的构造函数!

你有两个选择:要么是默认的构造函数添加到类:

​​

或致电new时所使用的参数:

opnode = new BinOpNode(op, left, right); 

祝您好运!

0

无类的有默认构造函数。
value返回评估表达式的结果,并且在构建表达式时需要传递表达式的必要部分作为其参数。
(目前还不清楚你如何指望能值5分配给二进制表达。)

你需要建立从叶一树(这将是常数),迈向根。
作为一个例子,这里的表达5 + 3

ConstNode five(5); 
ConstNode three(3); 
BinOpNode fiveplusthree('+', &five, &three); 
std::cout << fiveplusthree.value(); // Should print 8 
0

我认为这个问题是在你的main()函数的逻辑。

根据给定类别的定义,首先应该在表达式中为每个数字创建一个类型为ConstNode的对象。然后,您应该为该表达式中的每个运算符创建BinOpNode

顺便说一句,该表达式评估为18,而不是82!

像这样:

//3*(7+1)/4+(17-5) = 18 
int main() 
{ 
    BinOpNode *a, *b; 
    a = new BinOpNode('+', new ConstNode(7), new ConstNode(1)); 
    a = new BinOpNode('*', new ConstNode(3), a); 
    a = new BinOpNode('/', a, new ConstNode(4)); 
    b = new BinOpNode('-', new ConstNode(17), new ConstNode(5)); 
    b = new BinOpNode('+', a, b); 
    cout << b->value(); 
} 

PS:我们可以通过ConstNode类的对象时的ExpNode中的对象的BinOpNode作为ConstNode继承构造预计从ExpNode抽象基类。