在我的程序中,我有这样的类层次结构:
#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 <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class root
{
public:
virtual ~root() {}
virtual root add(const root& a) const=0;
virtual root add(const root& b) const=0;
};
class aa: public root
{
public:
aa() { }
aa(const aa& a) { }
virtual root add(const root& a) const
{ return root(new aa()); }
virtual root add(const root& b) const
{ return root(new bb()); }
};
class bb: public root
{
public:
bb() { }
bb(const bb& b) {}
virtual root add(const root& a) const
{ return root(new bb()); }
virtual root add(const root& b) const
{ return root(new bb()); }
};
int main(int argc, char **argv)
{
}
编辑类的错误:
/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 ===|