Visualizzazione dei risultati da 1 a 8 su 8

Discussione: [C++] Problema classi

  1. #1

    [C++] Problema classi

    Salve a tutti, ho un problema con le classi. In realtà è un problema legato agli array di oggetti.
    Questo è un esempio che stavo studiando da un libro:

    Codice PHP:
    #include <iostream>

    using namespace std;

    class 
    classe {
          
    int ab;
          public:
                 
    classe(int iint j);
                 
    int get_a();
                 
    int get_b();
                 };

    classe::classe(int iint j) { ij; }
    int classe::get_a() { return a; }
    int classe::get_b() { return b; }
     
    int main() {
        
        
    classe cla[2][2] = {
               
    cla(1,2), cla(2,2),
               
    cla(3,4), cla(8,10),
               };
        
    int i;
        
        for(
    i=0;i<4;i++) {
                         
    cout << cla[i][0].get_a() << '\n';
                         
    cout << cla[i][1].get_a() << '\n';
                         
    cout << cla[i][0].get_b() << '\n';
                         
    cout << cla[i][1].get_b() << '\n';
                         }
        
        
    system("Pause");
        return 
    0;

    Sto utilizzando Dev-C++... il problema si presenta in
    Codice PHP:
    classe cla[2][2] = {
               
    cla(1,2), cla(2,2),
               
    cla(3,4), cla(8,10),
               }; 
    Dev mi sottinea il fatto che l'oggetto cla della classe "classe" non può essere utilizzato come una funzione (in inglese: cla cannot be used as a function)

    Non so proprio cosa ci sia di sbagliato essendo alle prime armi con le classi...
    Qualcuno ha qualche idea?

    Grazie mille!

  2. #2

    Re: [C++] Problema classi

    Originariamente inviato da uniluigi

    Sto utilizzando Dev-C++... il problema si presenta in
    Codice PHP:
    classe cla[2][2] = {
               
    cla(1,2), cla(2,2),
               
    cla(3,4), cla(8,10),
               }; 
    Qualcuno ha qualche idea?
    sostituisci con:
    codice:
        classe cla[2][2] = { 
    	     classe(1,2), classe(2,2), 
    	     classe(3,4), classe(8,10), 
    	     };

  3. #3

    Re: Re: [C++] Problema classi

    Originariamente inviato da MacApp
    sostituisci con:
    codice:
        classe cla[2][2] = { 
    	     classe(1,2), classe(2,2), 
    	     classe(3,4), classe(8,10), 
    	     };
    Grazie mille! Però non capisco il perchè

  4. #4
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    perchè per costruire un oggetto di tipo "classe" usi il suo costruttore (chiamato appunto col nome della classe) e non il nome di una variabile (nel tuo esempio cla)

  5. #5
    Ah ok ora ho capito!! Allora sul libro è sbagliato

  6. #6
    altro errore da parte del libro, non ce la faccio più

    questo è l'errore:
    "main3.cpp: In member function `str_type str_type:perator+(str_type)':
    main3.cpp:25: error: no matching function for call to `str_type::str_type()'
    main3.cpp:6: note: candidates are: str_type::str_type(const str_type&)
    main3.cpp:20: note: str_type::str_type(char*)

    main3.cpp:29: error: no match for 'operator*' in '*temp'
    main3.cpp: In member function `str_type str_type:perator+(char*)':

    main3.cpp:37: error: no matching function for call to `str_type::str_type()'
    main3.cpp:6: note: candidates are: str_type::str_type(const str_type&)
    main3.cpp:20: note: str_type::str_type(char*)"


    Codice PHP:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;

    class 
    str_type {
          
    char string[80];
          public:
                 
    str_type(char str[80]); //costruttore
                 
                 
    str_type operator+(str_type str);
                 
    str_type operator=(str_type str);
                 
                 
    str_type operator+(char *str);
                 
    str_type operator=(char *str);
                 
                 
    void show_str();
                 };
          
    str_type::str_type(char str[80]) {
                            
    strcpy(stringstr);
                            }

    str_type str_type::operator+(str_type str) {
             
    str_type temp;
             
             
    strcpy(temp.stringstring);
             
    strcat(temp.stringstr.string);
             return 
    temp;
             }
    str_type str_type::operator=(str_type str) {
             
    strcpy(stringstr.string);
             return *
    this;
             }

    str_type str_type::operator+(char *str) {
             
    str_type temp;
             
             
    strcpy(temp.stringstring);
             
    strcat(temp.stringstr);
             return 
    temp;
             }

    str_type str_type::operator=(char *str) {
             
    strcpy(stringstr);
             return *
    this;
             }

    void str_type::show_str() {
         
    cout << string;
         } 
    Non so come risolvere...

  7. #7
    Originariamente inviato da uniluigi
    altro errore da parte del libro, non ce la faccio più

    questo è l'errore:
    "main3.cpp: In member function `str_type str_type:perator+(str_type)':
    main3.cpp:25: error: no matching function for call to `str_type::str_type()'

    codice:
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    
    
    str_type str_type::operator+(char *str) {
             str_type temp;
             
             strcpy(temp.string, string);
             strcat(temp.string, str);
             return temp;
             }
    Non so come risolvere...
    te lo dice il compilatore cosa non va:
    main3.cpp:25: error: no matching function for call to `str_type::str_type()'
    che vuol dire: manca il costruttore di default (quello senza argomenti) della classe str_type.

  8. #8
    Grazie mille! Ero cosi impacciato a trovare l'errore che non ho praticamente fatto caso al costruttore di default.

    Per concludere, io sto usando "Guida al C++" sec edizione di H.Schildt. E' un ottimo manuale per carità, ma pieno di errori (piccoli o gravi).
    Hai da consigliarmi qualche altro libro? Magari anche più completo. Grazie

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 © 2024 vBulletin Solutions, Inc. All rights reserved.