Visualizzazione dei risultati da 1 a 10 su 14

Visualizzazione discussione

  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2014
    Messaggi
    55

    [C++] Costruttori definiti fuori o dentro la classe

    Salve Ragazzi, non capisco perchè ho sempre problemi quando definisco i costruttori fuori dal corpo classe:
    codice:
    //Persona.h
    #ifndef PERSONA_H
    #define PERSONA_H
    #include "Data.h"
    using namespace std;
    
    class Persona{
    public:
        Persona();
        Persona(string,string,Data);
        virtual void print()=0;
    protected:
        string nome;
        string cognome;
        Data date;
    };
    
    class Studente : public Persona{
    public:
        Studente ();
        Studente (string a, string b, Data c, string d);
        virtual void print();
        ~Studente();
    protected:
        string CDL;
    };
    
    class Docente : public Persona{
    public:
        Docente ();
        Docente (string a,string b,Data c,string d);
        virtual void print();
        ~Docente();
    private:
        string Materia;
    };
    
    class StudenteSpecialista : public Studente{
    public:
        StudenteSpecialista ();
        StudenteSpecialista (string,string,Data,string,string);
        virtual void print();
        ~StudenteSpecialista();
    private:
        string LT;
    };
    
    #endif
    codice:
    //Persona.cpp
    #include <iostream>
    #include "Persona.h"
    #include "Data.h"
    using namespace std;
    
    
    Persona::Persona(){
        nome="-";
        cognome="-";
    }
    
    Persona::Persona(string a, string b, Data c){
        nome=a;
        cognome=b;
        date=c;
    }
    
    
    
    Studente::Studente(){
        Persona();
        CDL="-";
    }
    
    Studente::Studente(string a,string b,Data c,string d){
        Persona(a,b,c);
        CDL=d;
    }
    
    void Studente::print(){
        cout<<nome<<endl<<cognome<<endl<<date<<endl<<CDL<<endl<<endl;
    }
    
    Docente::Docente(){
        Persona();
        Materia="-";
    }
    
    Docente::Docente(string a, string b, Data c, string d){
        Persona (a,b,c);
        Materia=d;
    }
    
    Studente::~Studente(){
        cout<<"Distruttore"<<endl;
    }
    
    Docente::~Docente(){
        cout<<"Distruttore"<<endl;
    }
    codice:
    ||=== Build: Debug in ese5 (compiler: GNU GCC Compiler) ===|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp||In constructor 'Studente::Studente()':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp|21|error: cannot allocate an object of abstract type 'Persona'|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.h|6|note:   because the following virtual functions are pure within 'Persona':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.h|10|note:     virtual void Persona::print()|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp||In constructor 'Studente::Studente(std::string, std::string, Data, std::string)':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp|26|error: cannot allocate an object of abstract type 'Persona'|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.h|6|note:   since type 'Persona' has pure virtual functions|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp||In member function 'virtual void Studente::print()':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp|31|error: cannot bind 'std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
    c:\program files\codeblocks\mingw\lib\gcc\mingw32\4.8.1\include\c++\ostream|602|error:   initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Data]'|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp||In constructor 'Docente::Docente()':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp|35|error: cannot allocate an object of abstract type 'Persona'|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.h|6|note:   since type 'Persona' has pure virtual functions|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp||In constructor 'Docente::Docente(std::string, std::string, Data, std::string)':|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.cpp|40|error: cannot allocate an object of abstract type 'Persona'|
    C:\Users\Vinc\Desktop\Bill\Bill_ese_9\ese5\Persona.h|6|note:   since type 'Persona' has pure virtual functions|
    ||=== Build finished: 6 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
    il main è un semplice file di prova..
    codice:
    //main.cpp
    #include <iostream>
    #include "Persona.h"
    #include "Data.h"
    using namespace std;
    
    int main(){
        Studente l;
        Docente d;
    }
    Ultima modifica di MItaly; 15-01-2015 a 09:42 Motivo: Aggiunti tag CODE

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.