Abstrakte Klasse : ungültige abstract Rückgabetyp für die Funktion 'virtuelle...'
In meinem Programm habe ich solche Klassen-Hierarchie:
#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)
{
}
aber ich bin noch immer Fehler in der Kompilierung. Ich kann nicht ändern, meine Klassen-Hierarchie, aber es ist möglich, etwas zu machen, will ich hier?
BEARBEITET KLASSE:
#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)
{
}
Fehler bearbeitet Klasse:
/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 ===|
Ihr design ist ein wenig Durcheinander. Ihre Basis-Klasse muss über die abgeleiteten Typen, das ist nicht gut. Sollten Sie eine single-member-Funktion
Ok geändert, aber immer noch Fehler. Werfen Sie einen Blick auf meine editierte Frage.
Das Problem fachmännisch behandelt KerrekBS Antwort. Eine weitere alternative wäre die Rückgabe einer Referenz, aber dann müssen Sie sicher sein, dass es immer etwas zu finden.
Sie, dass der check, die du erwähnst, gilt für einen Zeiger auch richtig?
Nein, denn Sie kann null-Zeiger, die überprüft werden können, auf der rufenden Seite.
virtual root add(const root& a) const=0;
Ok geändert, aber immer noch Fehler. Werfen Sie einen Blick auf meine editierte Frage.
Das Problem fachmännisch behandelt KerrekBS Antwort. Eine weitere alternative wäre die Rückgabe einer Referenz, aber dann müssen Sie sicher sein, dass es immer etwas zu finden.
Sie, dass der check, die du erwähnst, gilt für einen Zeiger auch richtig?
Nein, denn Sie kann null-Zeiger, die überprüft werden können, auf der rufenden Seite.
InformationsquelleAutor Brian Brown | 2013-05-09
Du musst angemeldet sein, um einen Kommentar abzugeben.
Können Sie nicht zurück eine
root
von Wert, daroot
ist Abstrakt und so kann es niemals existieren keine Werte des Typsroot
.Möchten Sie vielleicht, um wieder eine Zeiger:
Was Sie haben, fühlt sich viel wie ein "Klon" oder "virtuelle Kopie" - Funktion, wenn:
Da fragte man über Verweise, hier ist eine andere Sache, die Sie tun konnte, obwohl es scheint ein bisschen sinnlos: Wählen Sie eine Referenz zu einem unter mehreren abgeleiteten Objekte und return-Basis-Referenz.
Verweis auf was? Ja, in meinem Beispiel könnten Sie versucht sein, zurück nur
x
odery
alsroot const &
, aber Sie würden begegnen Lebensdauer Probleme. Mit nicht-Konstanten Referenzen, wäre es besser, aber im Allgemeinen nicht sehr nützlich (nach alle, Sie wollen um neue Objekte zu erzeugen). Ich werde einige änderungen.Verweis auf root?
eine Klasse ist. Ich meine "Referenz auf die Objekt?".
InformationsquelleAutor Kerrek SB