[英]Compiler errors using inheritance in C++
I'm a newbie in C++.我是 C++ 的新手。 I've written an example code that illustrates the problem and causes compiler errors.
我编写了一个示例代码来说明问题并导致编译器错误。
class Quantifier;
class Node {
public:
virtual void Match(/* args */) = 0;
};
class QuantifiableNode : public Node {
public:
virtual void SetQuantifier(Quantifier *quantifier) = 0;
};
class NodeBase : public Node {
public:
void Match() override {
// Base behavior
}
};
class QuantifiableNodeImpl : public NodeBase, // Needed to inherit base behavior
public QuantifiableNode // Needed to implement QuantifiableNode interface
{
public:
void SetQuantifier(Quantifier *quantifier) override {}
void Method() {
this->Match();
}
};
int main() {
QuantifiableNodeImpl node;
node.Match();
return 0;
}
I get these errors:我收到这些错误:
main.cpp(27): error C2385: ambiguous access of 'Match'
main.cpp(27): note: could be the 'Match' in base 'NodeBase'
main.cpp(27): note: or could be the 'Match' in base 'Node'
main.cpp(32): error C2259: 'QuantifiableNodeImpl': cannot instantiate abstract class
main.cpp(20): note: see declaration of 'QuantifiableNodeImpl'
main.cpp(32): note: due to following members:
main.cpp(32): note: 'void Node::Match(void)': is abstract
main.cpp(5): note: see declaration of 'Node::Match'
main.cpp(33): error C2385: ambiguous access of 'Match'
main.cpp(33): note: could be the 'Match' in base 'NodeBase'
main.cpp(33): note: or could be the 'Match' in base 'Node'
As far as I understand, the compiler can't compile this code because class QuantifiableNodeImpl
inherits class NodeBase
and interface QuantifiableNode
which both have a method Match
( NodeBase
implements it from Node
, QuantifiableNode
inherits abstract method from Node
).据我了解,编译器无法编译此代码,因为 class
QuantifiableNodeImpl
继承 class NodeBase
和接口QuantifiableNode
两者都有一个方法Match
( NodeBase
从Node
实现它, QuantifiableNode
从Node
继承抽象方法)。 I need to have both of the interfaces Node
and QuantifiableNode
and use them in another code.我需要同时拥有
Node
和QuantifiableNode
这两个接口,并在另一个代码中使用它们。 Also, I need to have NodeBase
class in order to separate base functionality (in my real code I have many derivatives of NodeBase
).此外,我需要有
NodeBase
class 以分离基本功能(在我的真实代码中,我有许多NodeBase
的衍生物)。
Also, an object of class QuantifiableNodeImpl
can't be created too, the compiler says that it has an unimplemented abstract Match
method.此外,class
QuantifiableNodeImpl
的 object 也无法创建,编译器说它有一个未实现的抽象Match
方法。
So, what should I do?所以我该怎么做? Hope for your help!
希望得到您的帮助!
It seems you're not entirely familiar with diamond inheritance.看来您对钻石 inheritance 并不完全熟悉。
QuantifiableNodeImpl
has two Node
sub-objects, one via NodeBase
and one via QuantifiableNode
. QuantifiableNodeImpl
有两个Node
子对象,一个通过NodeBase
,一个通过QuantifiableNode
。 The second Node
subobject lacks a QuantifiableNode::Match
.第二个
Node
子对象缺少QuantifiableNode::Match
。
It seems that this might not be intentional: is QuantifiableNode
really supposed to be a Node
in its own right?看起来这可能不是故意的:
QuantifiableNode
真的应该是一个Node
本身吗? How is QuantifiableNode::Match
supposed to work? QuantifiableNode::Match
应该如何工作?
It might be that Quantifiable
is more accurately modelled as a mixin可能是
Quantifiable
更准确地建模为mixin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.