0

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

2
  • I dont understand why your operator overload is a friend function? Commented May 19, 2014 at 13:42
  • @Cool_Coder Because it's supposed to be? That's what my professor explained. Commented May 19, 2014 at 13:43

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.