Visualizzazione dei risultati da 1 a 4 su 4

Discussione: [C++]Problema Get-Set

  1. #1

    [C++]Problema Get-Set

    Ho questa lib mia:
    codice:
     // files : head.h 
    #include <string> 
    #ifndef HEAD_H 
    #define HEAD_H 
    
       // la struttura delle persona 
       class Persona 
       { 
          private: 
             char* nome;// il nome 
             char* cognome;//il cognome 
             int anno;//anno 
          public: 
             Persona(char*, char*, int); 
             Persona(); 
             char getNome(); 
             char getCognome(); 
             char setNome(char* nm); 
             char setCognome(char* cgn); 
             int setAnno(); 
             int getAnno(int ann); 
       }; 
    #endif
    codice:
     // files : Princ.cpp 
    #include <iostream> 
    #include <string> 
    #include "head.h" 
    
    using namespace std; 
    
    void main() 
    { 
       cout<<"\n"; 
    } 
    
    // ---------------------------------------- 
    // Definizioni 
    // ---------------------------------------- 
    // costruttore vuoto 
    Persona::Persona() 
    { 
    } 
    //costruttore con argomenti 
    Persona::Persona(char* nm, char* cgn, int ann=0) 
    { 
    nome = nm; 
    cognome = cgn; 
    anno = ann; 
    } 
    // ritorna il nome 
    char  Persona::getNome() 
    { 
    std::cout << nome; 
       return nome; 
    } 
    // ritorna il cognome 
    int Persona::getAnno() 
    { 
       std::cout << anno; 
       return anno; 
    } 
    // ritorna l'anno di nascita 
    char Persona::getCognome() 
    { 
       std::cout << cognome; 
       return cognome; 
    } 
    // setta il nome 
    char  Persona::setNome(char* nm) 
    { 
       nome = nm; 
       return nome; 
    } 
    // setta il cognome 
    int Persona::setAnno(int ann) 
    { 
       anno = ann; 
       return anno; 
    } 
    // setta l'anno di nascita 
    char Persona::setCognome(char* cgn) 
    { 
       cgn = cognome;    
       return cognome; 
    }
    Ma mi da:
    c:\documents and settings\admin\desktop\gest\princ.cpp(31) : error C2440: 'return' : cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\admin\desktop\gest\princ.cpp(35) : error C2511: 'getAnno' : overloaded member function 'int (void)' not found in 'Persona'
    c:\documents and settings\admin\desktop\gest\head.h(8) : see declaration of 'Persona'
    c:\documents and settings\admin\desktop\gest\princ.cpp(43) : error C2440: 'return' : cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\admin\desktop\gest\princ.cpp(49) : error C2440: 'return' : cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    c:\documents and settings\admin\desktop\gest\princ.cpp(53) : error C2511: 'setAnno' : overloaded member function 'int (int)' not found in 'Persona'
    c:\documents and settings\admin\desktop\gest\head.h(8) : see declaration of 'Persona'
    c:\documents and settings\admin\desktop\gest\princ.cpp(61) : error C2440: 'return' : cannot convert from 'char *' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    Ma nn capisco cosa sbaglio?
    Sono semplici get e set
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  2. #2
    Sei gnucco. Controlla il return type dichiarato dei metodi e gli argomenti che passi a setAnno e getAnno. Prima di fare questo genere di domande controlla quello che hai scritto.
    - "Boy, the food at this place is really terrible."
    - "Yeah, I know, and such ... small portions."

  3. #3
    risolto codi va bene?
    codice:
     // files : Princ.cpp 
    #include <iostream> 
    #include <string.h> 
    #include "head.h" 
    
    using namespace std; 
    
    void main() 
    { 
       cout<<"\n"; 
    } 
    
    // ---------------------------------------- 
    // Definizioni 
    // ---------------------------------------- 
    // costruttore vuoto 
    Persona::Persona() 
    { 
    } 
    // ritorna il nome 
    const char *Persona::getNome() 
    { 
       return nome; 
    } 
    // ritorna il cognome 
    const int Persona::getAnno() 
    { 
       return anno; 
    } 
    // ritorna l'anno di nascita 
    const char *Persona::getCognome() 
    {  
       return cognome; 
    } 
    // setta il nome 
    const char *Persona::setNome(char* nm) 
    { 
       *nome = *nm; 
       return nome; 
    } 
    // setta il cognome 
    const int Persona::setAnno(int ann) 
    { 
       anno = ann; 
       return anno; 
    } 
    // setta l'anno di nascita 
    const char *Persona::setCognome(char* cgn) 
    { 
       *cgn = *cognome;    
       return cognome; 
    }
    La stupidità umana e l'universo sono infinite.
    Della seconda non sono certo(Einstein)

    Gnu/Linux User

  4. #4
    Se non ti da errori e fa quello che vuoi che faccia va bene.
    - "Boy, the food at this place is really terrible."
    - "Yeah, I know, and such ... small portions."

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.