Mas Bagol
Mas Bagol

Reputation: 4617

Why "<<" operator can be inherited but ">>" operator can't?

Forgive me if it's just a silly question. I'm still new in C++ and this is my practice. I'm trying to create a simple game with Actor Object and Enemy Object that inherit from Unit Object. I put all of stats and overloaded operator that Actor and Enemy share in Unit class. Here's my simplified code for each class file:

Note: If this code too long, please just read the error on Actor.cpp and skip all of this. I write all of this because i don't know where's my error begin.

Unit.h

#ifndef UNIT_H_INCLUDED
#define UNIT_H_INCLUDED

#include <iostream>
#include <fstream>
#include <string>

class Unit{

        friend std::ostream& operator<<(std::ostream&, const Unit&);
        friend std::istream& operator>>(std::istream&, Unit&);

    public:
        Unit(std::string name = "");

        void setName(std::string);

        std::string getName();

        std::string getInfo();

        int attack(Unit&);

    protected:
        std::string name;
};

#endif

Unit.cpp

#include "Unit.h"

using namespace std;

Unit::Unit(string name){
    this->name = name;
}

void Unit::setName(string name){
    this->name = name;
}

string Unit::getName(){
    return name;
}

string Unit::getInfo(){
    string info;
    info = "Name\t: " + name;
    return info;
}

ostream& operator<<(ostream& output, const Unit& unit){

    output << unit.name << "\n";

    return output;
}
istream& operator>>(istream& input, Unit& unit){

    input >> unit.name;

    return input;
}

Actor.h

#ifndef ACTOR_H_INCLUDED
#define ACTOR_H_INCLUDED

#include "Unit.h"

class Actor: public Unit{
    public:
        void saveActor();
        void loadActor();
};

#endif

Actor.cpp

#include "Actor.h"

using namespace std;

void Actor::saveActor(){
    ofstream ofs("actor.txt");
    if(ofs.is_open()){
        ofs << this;    // This work well.
    }
    ofs.close();
}
void Actor::loadActor(){
    ifstream ifs("actor.txt");
    if(ifs.is_open()){
        ifs >> this;    // This is the error.
    }
    ifs.close();
}

This code is simplified, my real code includes HP, MP, atk, def, mag, agi with sets and gets for each field which is exactly like name field. That's why i need to overload "<<" and ">>" operators.

My IDE (Visual Studio) said:

error C2678: binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

My question is:

  1. Why Actor class can inherit overloaded insertion operator but can't inherit overloaded extraction operator?
  2. Is it a good idea to include those standard library in my header files or should I include those in cpp files?

The first question is my problem. The second is just my curiosity, my you all experienced programmer can give me an advice.

Sorry for my bad language. English is not my main language.

Upvotes: 0

Views: 105

Answers (1)

Barry
Barry

Reputation: 303277

You're streaming an Actor*

ofs << this;
ifs >> this;

instead of streaming an Actor:

ofs << *this;
ifs >> *this;

The ofs line works because it calls the overload of operator<< for printing pointers, not because it's calling your overload on Unit.

Upvotes: 8

Related Questions