Pure Virtual Function with Ifstream parameter, definition error?

时间:2017-04-10 03:23:08

标签: c++

Hi I have a base class that is abstract with this code, its a file called BaseBallPlayer.h :

    #ifndef BASEBALLPLAYER_H
    #define BASEBALLPLAYER_H

    #include <iostream>
    using namespace std;

    class BaseBallPlayer{


    protected:
        string name;
        int height;
        int weight;
    public:
    virtual void load_player(ifstream & read) = 0;  
    virtual ~BaseBallPlayer()=0;

I then have a derived class file Hitter.h which has

#ifndef HITTER_H
#define HITTER_H

#include <iostream>
#include "BaseBallPlayer.h"
using namespace std;

class Hitter : public BaseBallPlayer{
public:
virtual void load_player(ifstream &read);
~Hitter();
}

Now in my Hitter.cpp here is code

#include <iostream>
#include "Hitter.h"
#include <string>

using namespace std;
void Hitter::load_player(ifstream & read){

    read >> name >> height >> weight;

    batAvg(hits, atBats);
}

I define the virtual void load_player(ifstream &read) function but I keep getting an error where I have read >> And the error states no operator matches these operands >> Operand types are std::ifstream >> std::string

Basically the function will be called when I read in a file and store the info from the file in those variables. If possible I would like to do this in the function instead of writing separte code in main, but I cant use .open() with &ifstream. Anyway I'm just focused on this one error I have with the operands for now.

The reason why I am using a pure virtual function is because I have more than one derived class from my base class. I also have a pure virtual destructor in which later on I will add to the program with pointers and arrayList, but don't worry about that.

Please no vector solutions since I have never done too much of it.

0 个答案:

没有答案