下面的代码片段会产生一个错误:
#include <iostream>
using namespace std;
class A
{
public:
virtual void print() = 0;
};
void test(A x) // ERROR: Abstract class cannot be a parameter type
{
cout << "Hello" << endl;
}
除了替换外,是否有其他更好的解决方案/解决方法来应对这个错误?
virtual void print() = 0;
使用
virtual void print() = { }
编辑:我希望能够使用多态性(即A* x = new B() ; test(x);
)将任何扩展/实现基类A的类作为参数传递。
干杯
A
。这也会解决错误问题。仅通过值传递不起作用,原因不止一个。 - AnT stands with RussiaA
中将print()
保持为纯虚函数,同时在B
中实现它。将test
参数类型更改为@dasblinkenlight建议的类型。 - Mahesh