Am practicing C++ basic inheritance concepts, came across the need to print out a custom class object and wrote an overload, followed guide exactly yet still does not register use of the new operator for printing (<<).
Am wondering if typed incorrectly or had some declaration/initiation errors somewhere else?
no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘Choco’) std::cout<< "Choco value: " << child << 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!
}