【问题标题】:"Expected '}' at end of input". Why do I keep getting this error?“输入结束时应为'}'”。为什么我不断收到此错误?
【发布时间】:2013-10-21 06:52:49
【问题描述】:

我不是一个完整的新手,所以我理解它的含义等,但我似乎无法弄清楚 Eclipse 似乎认为我缺少什么神秘的括号。

(忽略我还没有构建我说过要在我的代码中构建的事实——我还没有做到这一点。)

#include "./BinaryTree.h"
using namespace ods;
typedef BTNode1 Node;
Node * buildNode(Node *p = NULL, Node *l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

typedef BTNode1 Node;
Node * buildNodeComplete(Node *p = NULL, Node*l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

typedef BTNode1 Node;
Node * buildNodeStringy(Node *p = NULL, Node*l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

int main() {
    // build random tree of 1023 nodes
    BinaryTree<Node> T;
    Node *u;
    T.root() = buildNode();
    int d;

    for (int i = 1; i < 1023; ++i) {
        T.randomWalk(u, d);
        if (d == 0) u->left = buildNode(u);
        else u->right = buildNode(u);
    }

    //build complete tree of 1023 nodes
    BinaryTree<BTNode1> C;
    Node *a;
    C.root() = buildNodeComplete();
    int g;

    for (int i = 1; i <1023; i++) {
        C.randomWalk(a, g);
        if (g == 0) a->left = buildNodeComplete(a);
        else a->right = buildNodeComplete(a);
    }

    //build stringy tree of 1023 nodes
    BinaryTree<Node> S;
    Node *q;
    S.root() = buildNodeStringy();
    int v;

    for (int i = 1; i <1023; i++) {
         S.randomWalk(q, v);
         if (v == 0) q->left = buildNodeStringy(q);
         else q->right = buildNodeStringy(q);
    }

    // measure random walks in T
    double sum=0;
    double sum1=0;
    double sum2=0;
    for (int i = 0; i < 1000; ++i) {
        T.randomWalk(u, d);
        sum += T.depth(u);
        C.randomWalk(a, g);
        sum1 += C.depth(a);
        S.randomWalk(q, v);
        sum2 += S.depth(q);
    }

    std::cout << "Average length of random walk through arbitrary tree is "
              << sum/1000;
    std::cout << " and lg(n+1) = lg(1024) = 10."
              << std::endl;

   //lab 5 part b
    std::cout << "Average length of random walk through complete tree is "
              << sum1/1000 << endl;
    std::cout << "Average length of random walk through stringy tree is "
              << sum2/1000;

    return 0;
}

【问题讨论】:

  • 检查头文件(太)
  • 如果你构建你会看到错误在哪里
  • include 语句中的./ 是多余的;在#include 语句中使用"" 默认首先搜索源文件的目录。
  • buildNodebuildNodeCompletebuildNodeStringy 是完全相同的函数。为什么要重复?还有为什么 typedef Node 3 次?
  • @harald 我正在buildNodeCompletebuildNodeStringy 中构建不同类型的树,但还没有完成。另外,我输入了 typedef 3 次,因为我没有输入错误,所以我决定不质疑它,就这样把它们留在了那里。

标签: c++ binary-tree


【解决方案1】:

应该是this bug in Eclipse fixed,但尝试在右括号后添加一个新行。过去需要在 .cpp 文件的末尾有一个换行符,但现在实际上不需要了。

另外,如果我曾经见过,这看起来像是一个数据结构类分配,所以祝你好运(它变得更难之前变得更容易。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多