在 C++ 中使用继承的编译器错误
Compiler errors using inheritance in C++
我是 C++ 的新手。我已经编写了一个示例代码来说明问题并导致编译器错误。
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;
}
我收到这些错误:
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'
据我了解,编译器无法编译此代码,因为 class QuantifiableNodeImpl
继承了 class NodeBase
和接口 QuantifiableNode
两者有一个方法 Match
(NodeBase
从 Node
实现它,QuantifiableNode
从 Node
继承抽象方法)。
我需要同时拥有 Node
和 QuantifiableNode
接口,并在另一个代码中使用它们。另外,我需要 NodeBase
class 来分离基本功能(在我的真实代码中,我有许多 NodeBase
的派生词)。
另外,classQuantifiableNodeImpl
的对象也不能创建,编译器说它有一个未实现的抽象Match
方法。
那么,我该怎么办?希望得到您的帮助!
看来你对钻石继承不是很熟悉。 QuantifiableNodeImpl
有两个 Node
子对象,一个通过 NodeBase
,一个通过 QuantifiableNode
。第二个 Node
子对象缺少 QuantifiableNode::Match
.
这似乎不是故意的:QuantifiableNode
本身真的应该是 Node
吗? QuantifiableNode::Match
应该如何工作?
Quantifiable
可能更准确地建模为 mixin
我是 C++ 的新手。我已经编写了一个示例代码来说明问题并导致编译器错误。
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;
}
我收到这些错误:
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'
据我了解,编译器无法编译此代码,因为 class QuantifiableNodeImpl
继承了 class NodeBase
和接口 QuantifiableNode
两者有一个方法 Match
(NodeBase
从 Node
实现它,QuantifiableNode
从 Node
继承抽象方法)。
我需要同时拥有 Node
和 QuantifiableNode
接口,并在另一个代码中使用它们。另外,我需要 NodeBase
class 来分离基本功能(在我的真实代码中,我有许多 NodeBase
的派生词)。
另外,classQuantifiableNodeImpl
的对象也不能创建,编译器说它有一个未实现的抽象Match
方法。
那么,我该怎么办?希望得到您的帮助!
看来你对钻石继承不是很熟悉。 QuantifiableNodeImpl
有两个 Node
子对象,一个通过 NodeBase
,一个通过 QuantifiableNode
。第二个 Node
子对象缺少 QuantifiableNode::Match
.
这似乎不是故意的:QuantifiableNode
本身真的应该是 Node
吗? QuantifiableNode::Match
应该如何工作?
Quantifiable
可能更准确地建模为 mixin