c++ : error: member access into incomplete type , 未使用的参数 [-Werror,-Wunused-parameter]

标签 c++ incomplete-type

我有以下代码:

class MyNode;

class MyCompare {
  public:
    bool operator()(MyNode *a, MyNode *b) {
        return a->q <= b->q ? true : false;
    }
};

class MyNode {

  public:

    double sum;    
    double q;

    StateNode *parent;
    std::priority_queue<MyNode, std::vector<MyNode>, MyCompare> children;
};

但是出现如下编译错误:

error: member access into incomplete type 'MyNode'
        return a->q <= b->q ? true : false;
                ^
..MyNode.h:35:7: note: forward declaration of 'MyNode'
class MyNode;
      ^
..MyNode.h:39:46: error: unused parameter 'b' [-Werror,-Wunused-parameter]
    bool operator()(MyNode *a, MyNode *b) {

知道我在这里做错了什么吗?谢谢!

最佳答案

如错误信息所述,a->qb->q,即class member access operator 的用法要求类型 MyNodecomplete type .仅前向声明是不够的。

The following types are incomplete types:

  • class type that has been declared (e.g. by forward declaration) but not defined;

Any of the following contexts requires class T to be complete:

  • class member access operator applied to an expression of type T;

您可以将operator()的定义移到MyNode的定义之后,此时MyNode就完成了。例如

class MyNode;

class MyCompare {
  public:
    bool operator()(const MyNode *a, const MyNode *b) const;
};

class MyNode {

  public:

    double sum;    
    double q;

    StateNode *parent;
    std::priority_queue<MyNode, std::vector<MyNode>, MyCompare> children;
};

bool MyCompare::operator()(const MyNode *a, const MyNode *b) const {
    return a->q < b->q;
}

关于c++ : error: member access into incomplete type , 未使用的参数 [-Werror,-Wunused-parameter],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53920276/

相关文章:

c++ - 模板参数类型被编译器视为完整的,但其定义尚不可见

C++ stat.h 类型不完整且无法定义

C错误: dereferencing pointer to incomplete type,结构

C++ ifstream 函数

c++ - 使用 GDAL 和 OpenCV 编写 RGB 16 位图像

c++ - 创建 C++ 程序时符号加载(源信息剥离)错误

c++ - 在 GNU/Linux 中使用键盘快捷键启动应用程序功能

c++ - 为什么不能在类声明(不完整类型)中使用 "is_base_of"?

c++ - 不完整的类 : Convert void* to pointer to class type via dynamic_cast

c++ - enable_if 添加具有默认参数的函数参数?