我有以下代码:
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->q
和b->q
,即class member access operator 的用法要求类型 MyNode
为 complete 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/