【问题标题】:c++ compiling: error: expected constructor, destructor or type conversion before '*'c ++编译:错误:'*'之前的预期构造函数、析构函数或类型转换
【发布时间】:2011-03-03 01:46:38
【问题描述】:

我一直在对此进行一些研究,并在 stackoverflow 上发现了一些关于类型可见性的类似问题,但这似乎不是完全相同的问题(或者至少几个小时后我是这么认为的正在努力)。

让我们集中注意力:

问题

C++ 编译器报告“abc.cpp:132: error: expected constructor, destructor, or type conversion before ‘*’ token”

报告问题的代码

template <class C, class I> ABC<C, I>::Node * ABC<C, I>::buscaTreuIRetornaMinim(Node **node) {
    if (*node == NULL) return NULL;
    if ((*node)->fesq != NULL) return buscaTreuIRetornaMinim(&(*node)->fesq);
    Node *q = *node;
    *node = *node->fdre;
    return q;
}

问题在第一行,函数头报告。到目前为止,我知道问题出在指定“节点 *”时,但它已经完全合格,所以我看不出问题出在哪里。

类定义的其余部分

class ABC {
public:
    ABC(void) : arrel(NULL), numelements(0), altura(0) { }
    void inserir(C pclau, I pinfo);
    void inordre(void);
    I consultar(C pclau);
    C minim(void);
    C maxim(void);
    void esborrar(C pclau);

private:
    class Node {
    public:
        C clau;
        I info;
        Node *fesq;
        Node *fdre;

        Node(C pclau, I pinfo, Node *pfesq = NULL, Node *pfdre = NULL) : clau(pclau), info(pinfo), fesq(pfesq), fdre(pfdre) { }
    };

    Node *arrel;
    Node *actual;
    int numelements;
    int altura;

    void inserir(C pclau, I pinfo, Node **node);
    void inordre(Node **node);
    I consultar(C pclau, Node **node);
    C minim(Node **node);
    C maxim(Node **node);
    void esborrar(C pclau, Node **node);
    Node * buscaTreuIRetornaMinim(Node **node);
};

另一方面,我可以确保其余功能功能齐全。这是我目前处理的唯一问题。

任何提示将不胜感激。提前感谢您的宝贵时间。

【问题讨论】:

    标签: c++ compiler-errors nested-class


    【解决方案1】:

    包含模板参数的限定类型名称必须以关键字 typename 为前缀:typename ABC&lt;C, I&gt;::Node *

    你可以阅读更多关于这个typename关键字here的必要性。

    【讨论】:

    • 就是这样!我一定会看一下建议的文件。非常感谢!
    【解决方案2】:

    看来你需要帮助编译器识别 Node 是一种类型

    试试这个:

    template <class C, class I> 
    typename ABC<C, I>::Node* ABC<C, I>::buscaTreuIRetornaMinim(Node **node) 
    {
      if(*node == NULL) return NULL;
      if((*node)->fesq != NULL) return buscaTreuIRetornaMinim(&(*node)->fesq);
      Node *q = *node;
      *node = *node->fdre;
      return q; 
    }
    

    【讨论】:

    • 谢谢 Millianz,这与 Jason 在上一条评论中建议的相同。添加'typename'关键字后一切正常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2016-03-14
    • 2014-05-04
    相关资源
    最近更新 更多