简体   繁体   English

为什么可以继承“ <<”运算符而不能继承“ >>”运算符?

[英]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. 我还是C ++的新手,这是我的实践。 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. 我将Actor和Enemy共享的所有统计信息和重载运算符放在Unit类中。 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. 注意:如果此代码太长,请仅阅读Actor.cpp上的错误并跳过所有这些。 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. 此代码经过简化,我的真实代码包括HP,MP,atk,def,mag,agi,并且每个字段的名称和名称都与set和get相同。 That's why i need to overload "<<" and ">>" operators. 这就是为什么我需要重载“ <<”和“ >>”运算符。

My IDE (Visual Studio) said: 我的IDE(Visual Studio)说:

error C2678: binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 错误C2678:二进制'>>':未找到采用'std :: istream'类型的左操作数的运算符(或没有可接受的转换)

My question is: 我的问题是:

  1. Why Actor class can inherit overloaded insertion operator but can't inherit overloaded extraction operator? 为什么Actor类可以继承重载的插入运算符,但不能继承重载的提取运算符?
  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. 英语不是我的主要语言。

You're streaming an Actor* 您正在串流Actor*

ofs << this;
ifs >> this;

instead of streaming an Actor : 而不是串流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 . ofs行之所以起作用,是因为它调用operator<<的重载来打印指针,而不是因为它调用了Unit的重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM