Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 17
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    115

    [C++]Implementazione funzione di una classe

    TextLine::TextLine(const string &text)
    {
    char *str, *p;
    str = new char[text.size()+1];
    strcpy(str, text.c_str());
    p=strtok(str, " ,.!?;)");
    while(p!=NULL)
    {

    p=strtok(NULL, " ,.!?;)");
    }

    delete[] str;

    }
    Questa è la funzione, essa serve a suddividere una linea di testo in token.Io devo pero farmi ritornare un valore string, ho anche una funsione getText(){string str; //*da implementare*// return str} e non so se posso consatenarle insieme!? Grazie per l'aiuto in anticipo!!!

  2. #2
    un costruttore non può ritornare nessun valore.
    Devi scrivere la stringa di ritorno in un membro della classe il cui valore lo farai ritornare attraverso la funzione membro getText() .
    ciao
    sergio

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    115
    Scusa la mia ignoranza ma nn ho capito molto bene!!
    Allora se io faccio in questo modo e giusto:


    TextLine::TextLine(const string &text)
    {
    char *str, *p;
    str = new char[text.size()+1];
    strcpy(str, text.c_str());
    p=strtok(str, " ,.!?;)");
    while(p!=NULL)
    {
    mTokens=p;
    p=strtok(NULL, " ,.!?;)");
    }

    delete[] str;

    }
    E mTokens è stato dichiarato in private nella classe TextLine, in questo modo poi posso usare mTokens in getToken()???
    grazie in anticipo!!!

  4. #4
    sì, le funzioni membro possono accedere ai membri privati

  5. #5
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    115
    Ok, però ora è sorto un'altro problema.... mToken è stato dichiarato con un vector<TextToken* > in cui TextToken è: enum TextToken(word, punteggiatura, spazi);
    come faccio ad allocare p su TextToken(se è così che si dice?!)????!!!!
    Grazie ancora

  6. #6
    devi usare la funzione membro push_back della classe vector

    http://www.cplusplus.com/reference/stl/vector/

    ciao
    sergio

  7. #7
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    115
    TextLine::TextLine(const string &text)
    {
    char *str, *p;
    str = new char[text.size()+1];
    strcpy(str, text.c_str());
    p=strtok(str, " ,.!?;)");
    while(p!=NULL)
    {
    mTokens.push_back(p);
    p=strtok(NULL, " ,.!?;)");
    }

    delete[] str;

    }
    L'ho scritta così è solo che nella compilazione mi da questo errore:
    ..\textAnalayzer\li2TextDoc.cpp no matching function for call to `std::vector<li2::TextToken*, std::allocator<li2::TextToken*> >:ush_back(char*&)'

    Guarda ti inserisco anche le due classi a cui faccio riferimento, magari ti può essere utile.

    namespace li2 {

    /*------------------------------------------------------------------------------
    class TextToken
    Rappresenta una sequenza di caratteri (token) appartenenente a una data categoria
    -----------------------------------------------------------------------------*/

    class TextToken {
    public:

    // TextToken::Type: elenco dei tipi token di interesse

    enum Type {
    Word, // parola
    SpaceSequence, // sequenza di spazi
    PunctuationMark // simbolo di punteggiatura
    } ;

    // TextToken::TextToken: costruttore parametrico
    // permette di costruire un oggetto TextToken a partire dal tipo e dal
    // valore del token
    // Il valore di un token è la sequenza di caratteri che lo compongono

    TextToken(Type type, const string &value)
    : mTtype(type), mValue(value) {}

    Type mTtype;
    string mValue;
    };

    /*------------------------------------------------------------------------------
    class TextLine
    Rappresenta una linea di testo
    La linea di testo è rappresentata mediante la sequenza di TextToken in essa
    contenuti
    -----------------------------------------------------------------------------*/

    class TextLine {
    public:

    // TextLine::TextLine: costruttore parametrico
    // Costruisce un oggetto TextLine a partire da una stringa
    // L'implementazione del costruttore suddivide la stringa text nella
    // sequenza di token che la compongono

    TextLine(const string &text);

    // costruttore di copia
    TextLine(const TextLine &textline);

    // distruttore
    ~TextLine();

    // operatore di assegnazione
    TextLine &operator=(const TextLine &textline);

    // TextLine::getText
    // restituisce il testo della linea composto a partire dalla giustapposizione dei token
    // contentuti nella linea di testo
    string getText();

    // TextLine::getTokenCount
    // restituisce il numero di token nella linea di testo
    size_t getTokenCount() const;

    // TextLine::getToken
    // restituisce l'i-esimo token nella linea di testo
    TextToken *getToken(size_t i) const;

    private:
    // mTokens contiene la sequenza di TextToken che compongono la linea di testo
    vector<TextToken *> mTokens;
    };

  8. #8
    E se fossi in te eviterei di mescolare codice C e codice C++, sia la STL sia la libreria Boost offrono degli ottimi strumenti per la tokenizzazione di stringhe (ed eventualmente per l'implementazione di parser).

    Bruce Eckel's Thinking in C++, 2nd Ed

    Some programmers consider strtok( ) to be the poorest design in the Standard C library because it uses a static buffer to hold its data between function calls. This means:

    1. You can’t use strtok( ) in two places at the same time
    2. You can’t use strtok( ) in a multithreaded program
    3. You can’t use strtok( ) in a library that might be used in a multithreaded program
    4. strtok( ) modifies the input sequence, which can produce unexpected side effects
    5. strtok( ) depends on reading in “lines”, which means you need a buffer big enough for the longest line. This produces both wastefully-sized buffers, and lines longer than the “longest” line. This can also introduce security holes. (Notice that the buffer size problem was eliminated in WordList.cpp by using string input, but this required a cast so that strtok( ) could modify the data in the string – a dangerous approach for general-purpose programming).

    For all these reasons it seems like a good idea to find an alternative for strtok( )
    "Se riesci a passare un pomeriggio assolutamente inutile in modo assolutamente inutile, hai imparato a vivere."

  9. #9
    Utente di HTML.it
    Registrato dal
    Jun 2007
    Messaggi
    115
    Il problema è che questo è un progetto per l'università e quindi mi devo attenere a quello che mi viene detto di usare. cmq tu nn sai come posso risolvere la cosa??
    Grazie cristian

  10. #10
    nel vector devi inserire un TextToken * non un char * oppure prevedere un costruttore del TextToken che accetti come argomento un char * e fare un cast

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.