Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在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; }_C++_Inheritance_Compiler Errors - Fatal编程技术网

在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; }

在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; },c++,inheritance,compiler-errors,C++,Inheritance,Compiler 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 instantia

我发现以下错误:

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'
据我所知,编译器无法编译此代码,因为类
QuantifiableNodeImpl
继承类
NodeBase
和接口
QuantiableNode
,两者都有一个方法
匹配
NodeBase
Node
实现,
QuantifiableNode
Node
继承抽象方法)。 我需要有两个接口
节点
可量化节点
,并在另一个代码中使用它们。此外,我需要有
节点库
类来分离基本功能(在我的实际代码中,我有许多
节点库
的派生)

另外,类
QuantifiableNodeImpl
的对象也不能创建,编译器说它有一个未实现的抽象
Match
方法


那么,我该怎么办呢?希望你能帮助我!

看来你对菱形继承并不完全熟悉。
QuantifiableNodeImpl
有两个子对象,一个是通过
NodeBase
的,另一个是通过
QuantifiableNode
。第二个
节点
子对象缺少
QuantifiableNode::Match

这似乎不是故意的:
QuantifiableNode
本身真的应该是一个
节点吗?
QuantiableNode::Match
应该如何工作


可能是
可量化
更准确地建模为一个

您似乎并不完全熟悉菱形继承。
可量化NodeImpl
有两个
节点
子对象,一个通过
节点基
和一个通过
可量化节点
。第二个
节点
子对象缺少
QuantifiableNode::Match

这似乎不是故意的:
QuantifiableNode
本身真的应该是一个
节点吗?
QuantiableNode::Match
应该如何工作


可能是
可量化的
更准确地建模为

您可能需要查找虚拟继承。相关:您可能需要查找虚拟继承。相关: