c++ - Bunch of errors while overloading the output operator -
i have been writing code , wanted overload << operator. declared operator friend function has access private variables should stay private! it's giving me lot of errors back. gives me problems not allow me access private data need. if comment out operator programm compiles fine. , sorry because language in code german.
header:
#pragma once class fahrzeug { private: int hg; double tank; double fst; double dsv; double ks; public: fahrzeug(); fahrzeug(const fahrzeug& brm); friend ostream& operator<<(ostream& os, const fahrzeug& obj); // <------ ~fahrzeug(); }; cpp:
#include "fahrzeug.h" #include <iostream> using namespace std; fahrzeug::fahrzeug() { hg = 180; tank = 50; fst = 45; dsv = 9; ks = 50000; } fahrzeug::fahrzeug(const fahrzeug& brm) { hg = brm.hg; tank = brm.tank; fst = brm.fst; dsv = brm.dsv; ks = brm.ks; } ostream& operator<<(ostream& os, const fahrzeug& brm) // <------ operator { os << "höchstgeschwindigkeit: " << brm.hg << "km/h." << endl; os << "volumen des tanks: " << brm.tank << "l." << endl; os << "füllstand des tanks: " << brm.fst << "l." << endl; os << "durchschnittlicher spritverbrauch: " << brm.dsv << "l/(100km)" << endl; os << "kilometerstand des fahrzeugs: " << brm.ks << "km." << endl; return os; } fahrzeug::~fahrzeug() { }
you forgot #include <iostream> in fahrzeug.h.
add
#include <iostream> using std::ostream; right after
#pragma once
Comments
Post a Comment