抽象对象不能声明
我遇到了抽象/虚拟类的问题,这里有一个问题的复制:
#include <iostream>
class A
{
protected:
virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;
};
class B
: public A
{
public:
virtual std::string ToString() { return ""; };
virtual void Write(std::string data) { };
};
class C
: public A
{
protected:
std::string getDateTime()
{
return "TODAY";
};
void Write(std::string data, bool addDate)
{
std::cout << "BasicClassA Write" << std::endl;
};
bool CheckFile()
{
std::cout << "BasicClassA CheckFile" << std::endl;
return true;
};
bool OpenFile(std::string path)
{
std::cout << "BasicClassA OpenFile" << std::endl;
return true;
};
void CloseFile()
{
std::cout << "BasicClassA CloseFile" << std::endl;
};
};
class D
: public B,
public C
{
public:
BasicClassB();
virtual ~BasicClassB();
std::string ToString()
{
return "BasicClassB tostring";
};
void Write(std::string data)
{
std::cout << "BasicClassB Write" << std::endl;
};
};
int main(int ac, char *av[])
{
BasicClassB b;
std::cout << b.ToString() << std::endl;
b.Write("");
return 0;
}
这有一个编译错误:
../src/main.cpp: In function 'int main(int, char**)':
../src/main.cpp:82:错误:无法将变量“b”声明为抽象类型“BasicClassB”
../src/main.cpp:64: 注意:因为以下虚函数在 'BasicClassB' 中是纯虚函数:
../src/main.cpp:13:注意:虚拟 std::string BaseClassA::getDateTime()
../src/main.cpp:14: 注意:virtual void BaseClassA::Write(std::string, bool)
../src/main.cpp:15: 注意:virtual bool BaseClassA::CheckFile()
../src/main.cpp:16: 注意:virtual bool BaseClassA::OpenFile(std::string)
../src/main.cpp:17: 注意:virtual void BaseClassA::CloseFile()
也许我在这里忽略了一点,但是 BaseClassA (BasicClassA)的实现应该包含这些函数,并且因为 BasicClassB 是从BasicClassA也是如此,它应该也包含这些函数吧?
我缺少什么?我应该怎么做才能编译这个?
[编辑] 我按照评论的建议更新了类名
澄清一下:我在 A 类中使用纯虚拟来强制任何子级实现这些功能。
看来虚拟继承是我所需要的,但是,在我的例子中,我似乎没有找到正确的方法......
目标是有几个“基”类,有点像接口,强制子级来实现这些功能,但这些子级的任何子级都应该继承重写的函数(就像虚拟继承一样)
但是,使用任意组合 Any 类:公共虚拟 Anyother { } 不起作用并且总是给出相同的编译错误(上面的错误)。也许我需要改变的不仅仅是继承中的虚拟?
I'm having a problem with abstract/virtual classes, a replication of the problem here:
#include <iostream>
class A
{
protected:
virtual std::string getDateTime() = 0;
virtual void Write(std::string data, bool addDate) = 0;
virtual bool CheckFile() = 0;
virtual bool OpenFile(std::string path) = 0;
virtual void CloseFile() = 0;
};
class B
: public A
{
public:
virtual std::string ToString() { return ""; };
virtual void Write(std::string data) { };
};
class C
: public A
{
protected:
std::string getDateTime()
{
return "TODAY";
};
void Write(std::string data, bool addDate)
{
std::cout << "BasicClassA Write" << std::endl;
};
bool CheckFile()
{
std::cout << "BasicClassA CheckFile" << std::endl;
return true;
};
bool OpenFile(std::string path)
{
std::cout << "BasicClassA OpenFile" << std::endl;
return true;
};
void CloseFile()
{
std::cout << "BasicClassA CloseFile" << std::endl;
};
};
class D
: public B,
public C
{
public:
BasicClassB();
virtual ~BasicClassB();
std::string ToString()
{
return "BasicClassB tostring";
};
void Write(std::string data)
{
std::cout << "BasicClassB Write" << std::endl;
};
};
int main(int ac, char *av[])
{
BasicClassB b;
std::cout << b.ToString() << std::endl;
b.Write("");
return 0;
}
This has a compile error:
../src/main.cpp: In function ‘int main(int, char**)’:
../src/main.cpp:82: error: cannot declare variable ‘b’ to be of abstract type ‘BasicClassB’
../src/main.cpp:64: note: because the following virtual functions are pure within ‘BasicClassB’:
../src/main.cpp:13: note: virtual std::string BaseClassA::getDateTime()
../src/main.cpp:14: note: virtual void BaseClassA::Write(std::string, bool)
../src/main.cpp:15: note: virtual bool BaseClassA::CheckFile()
../src/main.cpp:16: note: virtual bool BaseClassA::OpenFile(std::string)
../src/main.cpp:17: note: virtual void BaseClassA::CloseFile()
Perhaps I'm missing the point here, but the implementation of BaseClassA (being BasicClassA) should contain these functions, and since BasicClassB is subclassed from BasicClassA as well, it should also contain these functions?
What am I missing? What should I do to make this compile?
[edit]I updated the class names as suggested by the comment
For clarification: I used pure virtual in the class A to force any of the children to implement the functions.
It seems virtual inheritance is what I need, however, I don't seem to get the correct way on how to do this in my case...
The goal is to have several "base" classes, kind of like interfaces, forcing the children to implement the functions, but any children of those should inherit the overriden function (just like virtual inheritance)
However, using any combination of
class Any : public virtual Anyother { }
doesn't work out and always gives the same compile error (the one above). Perhaps I need to change more than just the virtual in the inheritance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
默认情况下,在 C++ 中它不会以这种方式工作 - 您需要钻石继承模式,但在 C++ 中您会获得单独的根:因此 BasicClassA 和 BaseClassB 每个都有自己的 BaseClassA (虚函数表和实例变量)。
您可能想使用虚拟继承。
为了更清楚地了解非虚拟继承:
这会产生输出:
此示例显示虚拟继承以及您想要的混合行为类型。
输出:
注意:C 和 B 的构造函数在设置 m_a 方面没有发言权,m_a 是 D() 构造函数初始化列表的控制器。
编辑:
将虚拟应用到您的代码中:
It doesn't work that way by default in C++ - You want a diamond inheritance pattern, but in C++ you get separate roots: So BasicClassA and BaseClassB each have their own BaseClassA (vtable and instance variables).
You probably want to use Virtual Inheritance.
For a clearer idea on non-virtual inheritance:
This produces the output:
This example shows virtual inheritance, and the kind of mix-in behaviour you want.
Output:
Note: The constructors from C and B don't get a say in setting m_a, which is controller by the D() constructor initialisation list.
EDIT:
Applying virtual to your code:
BaseClassA有5个纯虚函数。即使只有一个纯虚函数的类也是“抽象类”。纯虚函数(简而言之)的目的是禁止创建抽象类的对象。
为了实例化 BaseClassB,它需要定义在 BaseClassA 中声明为纯虚函数的所有 5 个函数。 (如果没有这些定义,BaseClassB 也会变成 Abstract,因此您无法从中创建对象)。
BaseClassA has 5 pure virtual functions. A class with even one pure virtual function is an "Abstract class". The purpose of pure virtual functions (in short) is to disallow creation of objects of the abstract class.
In order to instantiate BaseClassB, it needs to have definitions of all 5 functions which you declared pure virtual in BaseClassA. (In absence of these definitions, BaseClassB also becomes Abstract and hence you cannot create objects from it).
BasicClassB
仅派生自BaseClassA
,它是一个抽象类,因为这些方法:是纯虚拟。
错误消息非常清楚:为了能够实例化
BasicClassB
,您必须提供上述方法的实现。另请注意,
BasicClassB
中对Write
的定义:与
BaseClassA
中的定义不同:因此仍需要为
实现此方法>BasicClassB
变得可实例化。BasicClassB
only derives fromBaseClassA
which is an abstract class since those methods :Are pure virtual.
The error message is pretty clear: to be able to instantiate a
BasicClassB
you must provide an implementation for the forementioned methods.Also, note that your definition of
Write
inBasicClassB
:Differs from the one in
BaseClassA
:So this method still needs to be implemented for
BasicClassB
to become instantiable.事实上,您将“=0”添加到函数中意味着它们是纯虚拟的,并且必须在子类中实现。这显然不是你想要的。如果从基类中具有实现的函数中删除“=0”,它应该按预期工作。
The fact that you add "=0" to your functions means that they are purely virtual, and must be implemented in child classes. Which is obviously not what you want. If you drop the "=0" from the functions that have an implementation in the base class, it should be working as intended.