String



  • Naja ohne zu wissen, was das Endergebnis sein soll, wirds schwer. Sonst bleibt die Lösung wohl c_str oder std::copy



  • SeppJ hats dir ja eigentlich schon gesagt, du nimmst std::copy dafür musst du wissen was Iteratoren sind und wie man Header einbindet.



  • Hm, ob std::copy wirklich das richtige ist? Vielleicht ist hier eher sowas wie strlcpy gesucht, wobei ich mal vermuten würde, dass man das von Hand implementieren soll. Aber ohne Aufgabe kann man nur raten.



  • Byson schrieb:

    for (int i = 0; i<name.size(); i++){
    
    char m_name[i] = "Hallo";
    string name(m_name);
    
    }
    

    Würde das auch so funktionieren?

    Ei ei ei -.-
    Du hast jetzt schon 2 Hinweise c_str oder std::copy, lies die C++ Referenz darüber, sind sogar Beispiele dabei!



  • #include <string>
    #include <vector>
    #include <iostream>
    
    auto main() -> int {
    std::string const myString{"Hallo"};
    std::vector<char> myVec{myString.begin(), myString.end()};
    
    for (auto character : myVec)
    std::cout << "Individual character: " << character << std::endl;
    
    }
    

    so in der Art



  • Ich poste doch lieber die AUFGABE:
    [code="cpp"#ifndef CProtocol_h
    #define CProtocol_h

    #define MAXPAYLOAD 30
    #include <stdio.h>
    #include<iostream>
    using namespace std;

    enum protocolId_t {UNDEFINED,TEMPERATURE,HUMIDITY,PRESSURE,ASCII,ALL};
    class CProtocol {

    private:
    protocolId_t m_id = UNDEFINED;
    char m_payload [MAXPAYLOAD] ={0};
    unsigned int m_nextFreePos = 0;
    unsigned int m_crc = 0;
    unsigned int const calculateCrc();

    public:
    CProtocol();
    CProtocol( protocolId_t id, string payload,unsigned int crc);
    void setProtocolId(protocolId_t id);
    protocolId_t getProtocolId()const;
    bool addPayload(char data);
    void setCrc(unsigned int crc);
    string getTypeAsString(protocolId_t id)const;
    friend ostream& operator << (ostream& out,const CProtocol& rop);

    };
    ostream& operator << (ostream& out,const CProtocol& rop);
    #endif /* CProtocol_h */
    [/code]

    Im Konstruktor wollte ich m_payload = payload setzen .
    Das klappt allerdings nicht ,da es einmal ein string ist und einmal char array?

    Vector haben wir leider nicht gelernt

    [code="cpp"CProtocol::CProtocol(){
    for (int i = 0; i< MAXPAYLOAD; i++){

    m_payload[i]= 0;
    }

    };
    CProtocol::CProtocol(protocolId_t id,string payload, unsigned int crc){
    for (int i = 0; i<payload.size(); i++){
    keine ahnung

    }

    m_crc = crc;

    m_id = id;
    if(m_nextFreePos>MAXPAYLOAD){

    m_nextFreePos = MAXPAYLOAD;
    }

    };[/code]

    Vielleicht versteht ihr mein Problem jetzt besser?



  • #include "CProtocol.h"
    #include <stdio.h>
    #include<iostream>
    #include<string>
    using namespace std;
    
    CProtocol::CProtocol(){
        for (int i = 0; i< MAXPAYLOAD; i++){
    
            m_payload[i]= 0;
        }
    
    };
    CProtocol::CProtocol(protocolId_t id,string payload, unsigned int crc){
        for (int i = 0; i<payload.size(); i++){
            Keine Ahnung
    
        }
    
        m_crc = crc;
    
        m_id = id;
        if(m_nextFreePos>MAXPAYLOAD){
    
            m_nextFreePos = MAXPAYLOAD;
        }
    
    };
    

    header:

    #ifndef CProtocol_h
    #define CProtocol_h
    
    #define MAXPAYLOAD 30
    #include <stdio.h>
    #include<iostream>
    using namespace std;
    
    enum protocolId_t {UNDEFINED,TEMPERATURE,HUMIDITY,PRESSURE,ASCII,ALL};
    class CProtocol {
    
    private:
        protocolId_t m_id = UNDEFINED;
        char  m_payload [MAXPAYLOAD]  ={0};
        unsigned int m_nextFreePos = 0;
        unsigned int m_crc = 0;
        unsigned int  const calculateCrc();
    
    public:
        CProtocol();
        CProtocol( protocolId_t id, string payload,unsigned int crc);
        void setProtocolId(protocolId_t id);
        protocolId_t getProtocolId()const;
        bool addPayload(char data);
        void setCrc(unsigned int crc);
        string  getTypeAsString(protocolId_t id)const;
        friend ostream& operator << (ostream& out,const CProtocol& rop);
    
    };
    ostream& operator << (ostream& out,const CProtocol& rop);
    #endif /* CProtocol_h */
    

    Der code wurde oben nicht richtig dargestellt 🙂



  • Naja, so richtig Sinn macht das alles nicht. Aber gut, die Übungsaufgabe ist halt so gestellt.

    m_payload ist ein Puffer, der die Telegrammdaten aufnehmen soll, die im Konstruktor als std::string übergeben werden. Deine Aufgabe ist es jetzt, die Zeichen aus payload in den Puffer zu übertragen. Im Konstruktor ist das einfach, weil der Puffer noch leer ist und von Beginn an gefüllt werden kann. Du musst also diese Schritte erledigen:

    1. Prüfen, ob ausreichend Platz im Puffer ist. Die Länge von payload darf nicht größer sein als MAXPAYLOAD damit das der Fall ist.

    2. Die Einfügeposition in den Puffer merken, falls später zB über addPayload weitere Daten angehängt werden müssen.

    3. Die CRC-Prüfsumme für m_Payload berechnen

    Die Aussage, dass hier bei Hausaufgaben nicht gerne geholfen wird, ist pauschal falsch. Es gibt zwar häufig patzige Antworten in Hausaufgaben-Threads, aber nur deswegen, weil der Threadersteller sich keine Mühe gibt, keine Eigeninitiative zeigt und glaubt, er könne hier nach fertigen Lösungen fragen.



  • [code="cpp"CProtocol::CProtocol(protocolId_t id,string payload, unsigned int crc){
    for (int i = 0; i<payload.size(); i++){
    if(m_nextFreePos< MAXPAYLOAD&& payload.size()== MAXPAYLOAD){

    }

    }
    [/code]

    Ein bisschen was habe ich. ABer keine Ahnung wie ich weiter vorgehen soll?



  • Byson schrieb:

    CProtocol::CProtocol(protocolId_t id,string payload, unsigned int crc){
        for (int i = 0; i<payload.size(); i++){
            if(m_nextFreePos< MAXPAYLOAD&& payload.size()== MAXPAYLOAD){
                
                
            }
            
            
            
        }
    

    Ein bisschen was habe ich. ABer keine Ahnung wie ich weiter vorgehen soll?

    Genau genommen hast du leider nocht gar nichts, denn das da ist so unendlicher großer Murks, dass ich fast schon weinen möchte. Was genau soll das denn sein, bitte um Erklärung!



  • Die Länge von payload darf nicht größer sein als MAXPAYLOAD damit das der Fall ist

    Dachte das wäre die Antwort dafür 😃



  • Normalerweise prüft man das vor der Schleife statt bei jedem Schleifendurchlauf.



  • Ja aber trotzdem scheint es ja komplett falsch zu sein.
    Ich weiss es ja nicht wie man es macht ,sonst würde ich ja nicht fragen.



  • Und jetzt kommen wir langsam an den Punkt Eigeninitiative...

    Es wurden dir doch genügend Hinweise gegeben, was du machen musst. Und abgesehen von einer if -Abfrage hast du nichts geliefert. Den Inhalt eines std::strings in ein char -Array zu kopieren ist trivial. Wenn du das nicht hinbekommst schau in deinem Skript/Buch nach, das muss schon behandelt worden sein.



  • Alles klar .Hat sich erledigt .

    Könnt ihr mir nur erklären wie ich diesen Fehler bei Xcode weg bekomme ?
    Der macht mich verrückt
    CProtocol.cpp:93:72: Member function 'calculateCrc' not viable: 'this' argument has type 'const CProtocol', but function is not marked const

    [code="cpp"ostream& operator << (ostream& out,const CProtocol& rop){
    out <<rop.getTypeAsString(rop.m_id) << " " <<":"<< rop.m_payload << ""<<rop.calculateCrc()<<endl;
    if(rop.m_id== ASCII){
    for(int i = 0; i<MAXPAYLOAD; i++){
    out<< rop.m_payload[i] ;

    }
    }else{

    for(int i = 0; i<MAXPAYLOAD; i++){
    out<< (int)rop.m_payload[i] ;

    } }
    return out;
    }[/code]

    Könnt ihr mir nur erklären wie ich den Fehler weg bekomme ?
    Dann bin ich auch schnell weg 😃



  • ostream& operator << (ostream& out,const CProtocol& rop){
        out <<rop.getTypeAsString(rop.m_id) << " " <<":"<< rop.m_payload << ""<<rop.calculateCrc()<<endl;
        if(rop.m_id== ASCII){
            for(int i = 0; i<MAXPAYLOAD; i++){
                out<< rop.m_payload[i] ;
    
            }
        }else{
    
            for(int i = 0; i<MAXPAYLOAD; i++){
                out<< (int)rop.m_payload[i] ;
    
            }    }
        return out;
    }
    


  • Wie kriege ich den Fehler weg?



  • _Den_ Fehler bekommst du weg indem du _die_ Änderung machst.
    Merkste was?

    Oder anders gesagt: doof?

    EDIT: Die Fehlermeldung steht ja eh da, nur 2 Beiträge weiter oben. Übersehen.



  • ostream& operator << (ostream& out,const CProtocol& rop){
    

    Mach das

    const
    

    weg.



  • Ja aber in der Aufgabenstellung war das const ja so gegeben im Header ?
    Eine andere Möglichkeit gibt es nicht ?


Anmelden zum Antworten