-1

when compiling this simple code,i get this error:

[Error] no matching function for call to 'Studente::Registra()'

#include<iostream>
using namespace std;
class Anagrafica{
public: 
    int codice;
    string cognome;
    string nome;
    void Mostra();
    void Registra();
private:
    bool registrato;
};
void Anagrafica::Registra()
{
    registrato=true;
}
void Anagrafica::Mostra()
{
    if(registrato){
        cout<<nome<<endl
            <<cognome<<endl
                <<codice;     
    }
    else
        cout<<"\nNon registrato \n";
}

class Studente : public Anagrafica{
    int anno;
    char sezione;
    bool promosso;
public: 
    void Registra(int, char);
    void Promuovi();
    void Controlla();
    void Mostra();

    Studente(){
        promosso = false;
        anno = 0;
    }
};

void Studente::Mostra()
{
    Anagrafica::Mostra();
    if (anno) cout<<anno<<" "<<sezione<<endl;
}

void Studente::Registra(int a, char s)
{
    anno = a;
    sezione = s;
}

void Studente::Promuovi()
{
    promosso = true;
}

void Studente::Controlla()
{
    if(promosso) cout<<"\nStudente promosso\n";
        else cout<<"\nStudente NON promosso\n";
}

int main(){
    Studente stud1;
    char risp,sez;
    int anno;
    cout<<"\nNome: ";
    cin>>stud1.nome;
    cout<<"\nCognome: ";
    cin>>stud1.cognome;
    cout<<"Codice: ";
    cin>>stud1.codice;
    cout<<"Anno Scolastico:  ";
    cin>>anno;
    cout<<"Sezione: ";
    cin>>sez;
    stud1.Registra(anno,sez);
    stud1.Registra();
    cout<<"Promosso? (s/n) ";
    cin>>risp;

    if (risp=='s') stud1.Promuovi();
    stud1.Mostra();
    stud1.Controlla();
    return 0; 

}

i dont understand the problem, shouldn't the function overloading should take care of the call for 'Registra()'?

1

2 Answers 2

1

No, because in child class Studente you declare Registra() with the same name in base class Anagrafica. This is not function overloading. The function in class Studente hides the function in class Anagrafica.

When you call stud1.Registra(); it only searches for void Registra(int, char); in child class which does not have matching prototype.

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

Comments

1

You can fix this by adding using Anagrafica::Registra; in Studente.

That tells the compiler explicitly that you want the base class function to be considered when looking in Studente, rather than being hidden by your new function with the same name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.