在 C++ 中使用带头文件的抽象类
最近刚刚学习了 Java 中类继承的正确应用,并想将其应用到我用 C++ 编写的一些代码中。
以下是空 Node.cpp 的标头:
#ifndef Node_h
#define Node_h
#include <string>
using namespace std;
///////////////////////////////////////////////////////
//Class Declarations
class Array;
class Hash;
class Node;
class BodyNode;
class RealNode;
class CharNode;
class ComboNode;
class EndNode;
class HashNode;
class NodeFollower;
///////////////////////////////////////////////////////
//Class Definitions
class Node{
public:
virtual string toString(int info) = 0;
};
class BodyNode: public Node{
public:
};
class RealNode: public BodyNode{
protected:
Hash *wordHash, *addressHash;
Array *followers;
int count;
//Initialization
RealNode();
~RealNode();
//Hashes
BodyNode getNext(int address);
NodeFollower getWord(BodyNode *word);
NodeFollower getAddress(int address);
string toString(int info);
};
#endif
从这段代码中,我收到以下错误:
Node.h:43:11: error: invalid abstract return type for member function ‘BodyNode RealNode::getNext(int)’
Node.h:28:7: note: because the following virtual functions are pure within ‘BodyNode’:
Node.h:25:17: note: virtual std::string Node::toString(int)
在 google、stackoverflow 和 cplusplus 上查找了此内容,虽然我找到了很多围绕此问题的答案,但我无法'找不到我需要修复它的确切答案。我从中获取资源的 cplusplus.com 网站是:
http://www.cplusplus.com/ doc/tutorial/polymorphism/
此外,我在 stackoverflow 上找到的一些答案表明
using namespace std;
应该避免这种情况,这听起来有充分的理由,但我不知道如何包含字符串。我尝试
string::string
了每一个声明,也尝试过
using namespace std::string;
,但都无济于事。预先感谢您的帮助!
Just recently learned the correct application of inheritance of classes in Java and thought to apply it to some code I'm writing in C++.
The following is a header for an empty Node.cpp:
#ifndef Node_h
#define Node_h
#include <string>
using namespace std;
///////////////////////////////////////////////////////
//Class Declarations
class Array;
class Hash;
class Node;
class BodyNode;
class RealNode;
class CharNode;
class ComboNode;
class EndNode;
class HashNode;
class NodeFollower;
///////////////////////////////////////////////////////
//Class Definitions
class Node{
public:
virtual string toString(int info) = 0;
};
class BodyNode: public Node{
public:
};
class RealNode: public BodyNode{
protected:
Hash *wordHash, *addressHash;
Array *followers;
int count;
//Initialization
RealNode();
~RealNode();
//Hashes
BodyNode getNext(int address);
NodeFollower getWord(BodyNode *word);
NodeFollower getAddress(int address);
string toString(int info);
};
#endif
From this code, I get the following error:
Node.h:43:11: error: invalid abstract return type for member function ‘BodyNode RealNode::getNext(int)’
Node.h:28:7: note: because the following virtual functions are pure within ‘BodyNode’:
Node.h:25:17: note: virtual std::string Node::toString(int)
Looked this up all over google, stackoverflow, and cplusplus, and while I find a lot of answers that surround this concern, I couldn't find the exact answer that I needed to fix it. The cplusplus.com site that I was sourcing from is:
http://www.cplusplus.com/doc/tutorial/polymorphism/
Additionally, a few of the answers I found on stackoverflow indicated that
using namespace std;
is to be avoided, and it sounds like for good reason, but I can't figure out how to include string. I tried
string::string
for each declaration, and I tried
using namespace std::string;
but both were to no avail. Thank you in advance for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的第一个问题是 BodyNode 继承自具有
toString()
纯虚函数的常规 Node。如果它是纯虚拟的(=0),那么您必须在所有派生类中为它提供定义,但您没有。因此,由于您没有在
BodyNode
中提供纯虚函数定义,因此它也是一个抽象类且不可实例化(抽象类有 1 个或多个纯虚方法,因此无法实例化) 。您还遇到一个问题,即
getNext()
返回BodyNode
类型,并且由于BodyNode
是抽象的,因此这是不可能的 - 您可以返回指向它的指针而不是按复制值,但我认为您真正的问题是它首先不应该是抽象的。在 BodyNode 中提供默认的
toString()
函数来解决您的问题。Your first problem is that BodyNode inherits from regular Node which has a
toString()
pure-virtual function. If it's pure-virtual (the =0) then you MUST provide a definition for it in all derived classes, and you have not.So, since you have not provided the pure-virtual function definition within
BodyNode
, it is also an abstract class and uninstantiable (an abstract-class has 1 or more pure-virtual methods so it cannot be instantiated).You also have an issue that
getNext()
returns aBodyNode
type, and sinceBodyNode
is abstract, this isn't possible - you COULD return a pointer to it instead of a by-copy-value, but I think your real problem is that it shouldn't be abstract in the first place.Provide a default
toString()
function in BodyNode to fix your problem.不能使用抽象类作为函数返回类型。但您可以返回指针或对它的引用,如以下声明所示:
您可以将字符串声明为:
You cannot use abstract class as a function return type. But you can return pointer or reference to it, like in following declarations:
You can declare your string as:
由于
BodyNode
也是一个抽象类,因此您无法从getNext
返回它。您必须返回一个引用或一个指针。Since
BodyNode
is also an abstract class, you can not return it fromgetNext
. You either have to return a reference or a pointer.BodyNode
也是一个抽象类,因为 - 从抽象类派生的类必须实现纯虚函数,否则它们也是抽象类。您无法返回
BodyNode
类型的对象。您只能返回一个指针或引用。请参阅此 -http://msdn.microsoft.com/en-us/library/ c8whxhf1.aspx
BodyNode
is also an abstract class because - Classes derived from the abstract class must implement the pure virtual function or they, too, are abstract classes.You can not return an object of type
BodyNode
. You can only return a pointer or a reference. Refer this-http://msdn.microsoft.com/en-us/library/c8whxhf1.aspx