简体   繁体   中英

Cannot call member function std::string Sportist::getIme() without object

I have a class A:

class Sportist{
    private:
        string ime;
        int godina_na_ragjanje;
        int godisna_zarabotuvacka_EUR;
    public:
        Sportist(string i, int g_n_r, int g_z_EUR){
            ime = i;
            godina_na_ragjanje = g_n_r;
            godisna_zarabotuvacka_EUR = g_z_EUR;
        }
        string getIme(){
            return ime;
        }
        int getGodinaNaRagjanje(){
            return godina_na_ragjanje;
        }
        int getGodisnaZarabotuvackaEUR(){
            return godisna_zarabotuvacka_EUR;
        }
};

And class B using the class A as public:

class Fudbaler:public Sportist{
    private:
        int broj_na_odigrani_natprevari;
        int danocna_stapka;
    public:
        Fudbaler(string ime, int godina, int zarabotuvacka, int b, int d)
            :Sportist(ime, godina, zarabotuvacka)
        {
            broj_na_odigrani_natprevari = b;
            danocna_stapka = d;
        }
        float danok(){
            return getGodisnaZarabotuvackaEUR() * danocna_stapka;
        }
        friend ostream& operator<<(ostream &os, Fudbaler F){
            return os << "Ime: " << getIme() << endl
                      << "Godina na raganje: " << getGodinaNaRagjanje() << endl
                      << "Godisna zarabotuvacka(EUR): " << getGodisnaZarabotuvackaEUR() << endl
                      << "Danok sto treba da plati: " << danok();
        }
};

And I get 4 errors as described in title in these lines:

        return os << "Ime: " << getIme() << endl
                  << "Godina na raganje: " << getGodinaNaRagjanje() << endl
                  << "Godisna zarabotuvacka(EUR): " << getGodisnaZarabotuvackaEUR() << endl
                  << "Danok sto treba da plati: " << danok();

cannot call member function 'std::string Sportist::getIme()' without object

cannot call member function 'int Sportist::getGodinaNaRagjanje()' without object

cannot call member function 'int Sportist::getGodisnaZarabotuvackaEUR()' without object

cannot call member function 'float Fudbaler::danok()' without object

i would say the function should be changed to

friend ostream& operator<<(ostream &os, Fudbaler F){
            return os << "Ime: " << F.getIme() << endl
                      << "Godina na raganje: " << F.getGodinaNaRagjanje() << endl
                      << "Godisna zarabotuvacka(EUR): " << F.getGodisnaZarabotuvackaEUR() << endl
                      << "Danok sto treba da plati: " << F.danok();
        }

I am not shure about operator overloading for the std::streams . i usually have done that outside of the class. From your error messages, you need to use the passed Fudbaler variable to access the methods of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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