Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    98

    C/C++ - Metodo di setting non funzionante

    Raga guardate un po' questo codice:
    base.h
    codice:
    #ifndef _BASE_
    #define _BASE_
    
    #include <string>
    
    class base
    {
        private:
            
            char c[10];
            char* c2;
            std::string stringa;
    
        public:
    
            base();
            ~base();
            void setC(char[]);
            void setC2(char*);
            void setStringa(std::string);
            char getC();
            char* getC2();
            std::string getStringa();
    };
    
    #endif // _BASE_
    base.cpp
    codice:
    #include "base.h"
    #include <string>
    
    using namespace std;
    
    base::base()
    {
    	strcpy(c, "c1");
    	c2 = "c2";
    	stringa = "stringa";
    }
    
    base::~base(){};
    
    void base::setC(char c1[10])
    {
    	strcpy(c,c1);
    }
    
    void base::setC2(char* c)
    {
    	c2 = c;
    }
    
    void base::setStringa(string s)
    {
    	stringa = s;
    }
    
    char base::getC()
    {
    	char tmp[10];
    	strcpy(tmp,c);
    	return tmp[10];
    }
    
    char* base::getC2()
    {
    	return c2;
    }
    
    string base::getStringa()
    {
    	return stringa;
    }
    il problema è il metodo setC, infatti se nel main.cpp scrivo:
    codice:
    #include "base.h"
    #include <iostream.h>
    #include <string>
    
    void main()
    {
        base a;
    
        a.setStringa("stringa");
        a.setC("c1");
        a.setC2("c2");
        
        cout << "a.c:" << a.getC() << endl;
        cout << "a.c2:" << a.getC2() << endl;
        cout << "a.stringa:" << a.getStringa().c_str() << endl;
        
    }
    come risultato ho:
    a.ccarattere strano);
    a.c2: c2
    a.stringa: stringa
    Che errore ho fatto secondo voi?

  2. #2
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    hai postato il .h al posto del cpp

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    98
    pardon

  4. #4
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    perchè c è un pontatore ad un array di stringhe...

    per avere il valore devi iterarlo

  5. #5
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,320
    Controlla il metodo getC().
    Tu fai ritornare un carattere. Che carattere? L'undicesimo di un array di 10 elementi. Ricorda che in C (e C++ ovviamente) gli array sono indicizzati a partire da 0, quindi con un array di 10 elementi, l'ultimo è il 9.
    codice:
    char base::getC()
    {
    	char tmp[10];
    	strcpy(tmp,c);
    	return tmp[9];   // Questo è il decimo elemento!
    }
    Se poi il tuo intento era quello di ritornare la stringa intera, allora dovrai modificare la firma e il codice in questo modo:
    codice:
    // Firma:
    char[] getC();
    
    // Codice:
    char[] base::getC()
    {
    	char tmp[10];
    	strcpy(tmp,c);
    	return tmp;
    }
    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  6. #6
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    98
    ho provato a fare come hai detto ma mi ritorna tutti questi errori:



    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(21) : error C2059: syntax error : '['
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(21) : error C2238: unexpected token(s) preceding ';'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(30) : warning C4091: '' : ignored on left of 'char' when no variable is declared
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(30) : error C2143: syntax error : missing ';' before '['
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(30) : error C2143: syntax error : missing ';' before '['
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(30) : error C2039: 'getC' : is not a member of 'base'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(7) : see declaration of 'base'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(31) : error C2143: syntax error : missing ';' before '{'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.cpp(31) : error C2447: missing function header (old-style formal list?)
    main.cpp
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(21) : error C2059: syntax error : '['
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(21) : error C2238: unexpected token(s) preceding ';'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\main.cpp(13) : error C2039: 'getC' : is not a member of 'base'
    c:\users\xxx\desktop\intecs\ricapitolando\arraycar atteri\base.h(7) : see declaration of 'base'
    Error executing cl.exe.

    ArrayCaratteri.exe - 10 error(s), 1 warning(s)

  7. #7
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    non potevi essere più preciso..

    quando ritorni in array non lo ritorni come lo dichiari "tmp[10]" ma lo ritorni come puntatore all'area di memoria "tmp"..

    ma perchè invece di copiare l'arrey e ritornarlo non fai "return c;"?

  8. #8
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    98
    dovrei quindi modificare in questo modo?


    char* getC();


    char* base::getC()
    {
    char tmp[10];
    strcpy(tmp,c);
    return tmp;
    }

  9. #9
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    prova con:

    // Codice:
    char[] base::getC()
    {
    char[] tmp;
    strcpy(tmp,c);
    return tmp;
    }

    oppure con:

    char[] base::getC()
    {
    return c;
    }

    con in base: "char[] c;" al posto di "char c[10];"

  10. #10
    Utente di HTML.it
    Registrato dal
    May 2007
    Messaggi
    98
    nada de nada

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.