C++ 抽象类:成员函数的抽象返回类型无效‘;虚拟…’;
在我的程序中,我有这样的类层次结构:C++ 抽象类:成员函数的抽象返回类型无效‘;虚拟…’;,c++,virtual,abstract-class,C++,Virtual,Abstract Class,在我的程序中,我有这样的类层次结构: #include <iostream> #include <cmath> #include <sstream> using namespace std; class aa; class bb; class root { public: virtual ~root() {} virtual root add(const aa& a) const=0; virtual root add(co
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class aa;
class bb;
class root
{
public:
virtual ~root() {}
virtual root add(const aa& a) const=0;
virtual root add(const bb& a) const=0;
};
class aa: public root
{
public:
aa() { }
aa(const aa& a) { }
virtual root add(const aa& a) const
{ return root(new aa()); }
virtual root add(const bb& a) const
{ return root(new bb()); }
};
class bb: public root
{
public:
bb() { }
bb(const bb& b) {}
virtual root add(const aa& a) const
{ return root(new bb()); }
virtual root add(const bb& a) const
{ return root(new bb()); }
};
int main(int argc, char **argv)
{
}
不能按值返回
根
,因为根
是抽象的,因此不可能存在任何类型的根
值
您可能希望返回一个指针:
#include <memory>
std::unique_ptr<root> do_you_feel_lucky(aa const & x, bb const & y)
{
if (rand() % 2 == 0)
return { new aa(x) };
else
return { new bb(y) };
}
你的设计有点乱。您的基类需要了解派生类型,这并不好。您应该考虑使用单个成员函数<代码>虚拟根添加(const root和a)const=0;<代码>@juanchopanza:Ok更改了它,但仍然有错误。看看我编辑过的问题,这个问题在KerrekBS的回答中得到了很好的处理。另一种选择是返回一个引用,但是你必须确保总是有东西可以引用。@juanchopanza你提到的检查也适用于指针对吗?@Koushik否,因为你可以返回一个空指针,可以在调用方进行检查。+1。但这是因为它不能构造抽象类的对象吗?我们可以通过引用返回吗?@Koushik:引用什么?是的,在我的示例中,您可能会尝试只返回
x
或y
作为根常量&
,但您会遇到生存期问题。使用非常量引用会更好,但通常不是很有用(毕竟,您希望创建新对象)。我会做一些编辑。@Koushik:root
是一个类。我的意思是“参考哪个对象?”。
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘virtual root root::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: with ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: invalid abstract return type for member function ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: because the following virtual functions are pure within ‘root’:|
/home/brian/Desktop/Temp/Untitled2.cpp|10|note: virtual root root::add(const root&) const|
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: invalid abstract return type for member function ‘virtual root root::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: ‘virtual root aa::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: with ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|20|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|21|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root aa::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|22|error: invalid abstract return type for member function ‘virtual root aa::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected primary-expression before ‘(’ token|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected type-specifier before ‘bb’|
/home/brian/Desktop/Temp/Untitled2.cpp|23|error: expected ‘)’ before ‘bb’|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: ‘virtual root bb::add(const root&) const’ cannot be overloaded|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: with ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|32|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|33|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root bb::add(const root&) const’:|
/home/brian/Desktop/Temp/Untitled2.cpp|34|error: invalid abstract return type for member function ‘virtual root bb::add(const root&) const’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
/home/brian/Desktop/Temp/Untitled2.cpp|35|error: cannot allocate an object of abstract type ‘root’|
/home/brian/Desktop/Temp/Untitled2.cpp|6|note: since type ‘root’ has pure virtual functions|
||=== Build finished: 38 errors, 0 warnings ===|
#include <memory>
std::unique_ptr<root> do_you_feel_lucky(aa const & x, bb const & y)
{
if (rand() % 2 == 0)
return { new aa(x) };
else
return { new bb(y) };
}
struct Base
{
virtual std::unique_ptr<Base> clone() const = 0;
};
struct Derived : Base
{
virtual std::unique_ptr<Base> clone() const
{
return { new Derived(*this); }
}
};
root & pick_one_from_two(aa & x, bb & y)
{
return rand() % 2 == 0 ? x : y;
}