Übe C++ grundlegende Vererbungskonzepte, kam über die Notwendigkeit, ein benutzerdefiniertes Klassenobjekt auszudrucken und schrieb eine Überladung, gefolgt Leitfaden genau, aber immer noch nicht registriert die Verwendung des neuen Betreibers zum Drucken (< <).Drucken Überlastungsfehler des Bedieners?
Ich wundere mich, wenn falsch eingegeben oder hatte einige Erklärung/Initiation Fehler woanders?
keine Übereinstimmung für ‚Operator < <‘ (Operandentypen sind ‚std :: basic_ostream‘ und ‚Choco‘) std :: cout < < "Choco-Wert:" < < Kind < < endl;
#include <iostream>
using namespace std;
class Sweets {
public:
// pure virtual, child MUST implement or become abstract
// Enclosing class becomes automatically abstract - CANNOT instantiate
virtual bool eat() = 0;
};
// public inheritance : public -> public, protected -> protected, private only accessible thru pub/pro members
class Choco : public Sweets {
public:
bool full;
Choco() {
full = false;
}
// Must implement ALL inherited pure virtual
bool eat() {
full = true;
}
// Overload print operator
bool operator<<(const Choco& c) {
return this->full;
}
};
int main() {
// Base class object
//sweets parent;
Choco child;
// Base class Ptr
// Ptr value = address of another variable
Sweets* ptr; // pointer to sweets
ptr = &child;
std::cout<< "Sweets* value: " << ptr << endl;
std::cout<< "Choco address: " << &child << endl;
std::cout<< "Choco value: " << child << endl; // Printing causes error!
}
In Verbindung stehend: https://Stackoverflow.com/q/4421706/1896169 – Justin
in Verbindung stehend: https://Stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream –
@ jamarcus_13 Dieser Operator bool operator << (const Choco & c) { zurückgeben-> voll; } macht keinen Sinn, zumindest weil der Parameter c nicht verwendet wird :) –