String



  • Byson schrieb:

    Wo habe ich das hier falsch gemacht ?
    Array indices starten bei 0 und gehen bis size-1, d.h. m_payload[MAXPAYLOAD] ist ein out-of-range Zugriff, denn das letzte Element ist m_payload[MAXPAYLOAD-1]. Die Bedingung lautet ja auch nextpos<MAXPAYLOAD, da hast du falsch umgedreht.

    Ich schreibe mal einen deiner Schnipsel um

    bool CProtocol::addPayload(char data){
    
        if(m_nextFreePos > MAXPAYLOAD){
            return false;
        }
        else { // if (m_nextFreePos <= MAXPAYLOAD), also auch == MAXPAYLOAD, common off-by-one error
            m_payload[m_nextFreePos] = data;
            return true;
    
        }
    };
    

    Oder hier

    if(m_nextFreePos > MAXPAYLOAD){
    
        m_nextFreePos = MAXPAYLOAD;
    }
    

    Ich habe den code so umgedreht ,aber das zeigt dann Fehler an ?

    BEI m_crc wird kein Fehler angezeigt?

    Welchen Code und welche Fehler?

    cannot assign to non-static data member within const member function 'calculateCrc'

    Ich würde raten es liegt an dieser Zeile

    m_nextFreePos++;
    

    Wenn die Berechnung des crc so stimmt, dann würde ich einfach schreiben:

    unsigned CProtocol::calculateCrc() const
    {
        return m_id + m_payload[m_nextFreePos];
    }
    

    Ich erwarte von einer "calculate" Funktion, dass diese Funktion etwas berechnet und zurück gibt, und sonst keinerlei Nebeneffekte hat, außer evtl. irgendwas soll gecached werden, d.h. man merkt sich das Ergebnis und wenn sich beim nächsten Aufruf nichts geändert hat gibt man das gespeicherte Ergebnis zurück, anstatt die Berechnung neu durchzuführen.



  • Hallo Harte Ware .

    Ich habe deine Änderungen gemacht ,aber immer noch Fehlermeldung 😃

    #include "CProtocol.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++){
               m_payload[i] = payload [i];
        }
    
        m_crc = crc;
    
        m_id = id;
        if(m_nextFreePos>MAXPAYLOAD){
    
            m_nextFreePos = MAXPAYLOAD;
        }
    
    };
    
    void CProtocol::setCrc(unsigned int crc){
    
        crc = m_crc;
    };
    void CProtocol::setProtocolId(protocolId_t id){
    
        m_id = id;
    };
    protocolId_t CProtocol::getProtocolId()const{
    
        return m_id;
    };
    
    bool CProtocol::addPayload(char data){
    
    	  if(m_nextFreePos > MAXPAYLOAD){ 
    	        return false; 
    	    } 
    	    else { if (m_nextFreePos <= MAXPAYLOAD){
    	        m_payload[m_nextFreePos] = data; 
    	        return true; 
    
    	    } 
    	};
    
    string  CProtocol::getTypeAsString( protocolId_t id)const{
    
        switch(id){
            case UNDEFINED: return "UNDEFINED";
            case TEMPERATURE: return "TEMPERATURE";
            case HUMIDITY: return "HUMIDITY";
            case PRESSURE: return "PRESSURE";
            case ASCII: return "ASCII";
            case ALL: return "ALL";
    
            default: return "Unbekannter Typ";
    
        }
    
    }
    unsigned int  CProtocol::calculateCrc()const{
    
    	return m_id + m_payload[m_nextFreePos];
    }
    
    ostream& operator << (ostream& out,const CProtocol& rop){
        out <<rop.getTypeAsString(rop.m_id) << " " <<":"<< rop.m_payload << ""<<rop.calculateCrc();
        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;
    }
    

    Description Resource Path Location Type
    cannot assign to non-static data member within const member function 'calculateCrc' CProtocol.cpp /CProtokol line 79 C/C++ Problem
    Description Resource Path Location Type
    cannot assign to non-static data member within const member function 'calculateCrc' CProtocol.cpp /CProtokol line 80 C/C++ Problem

    Ich hatte bereits auch tutoren befragt ,die auch nicht auf die Lösung des Problemes gekommen sind .

    Bitte um Hilfe

    Ich will das das Programm endlich kompiliert 😃



  • Die Fehlermeldung paßt nicht zu deinem Code! Hast du wirklich alles neu kompilieren lassen?



  • Stimmt jetzt habe ich die Fehler
    Description Resource Path Location Type
    expected '}' CProtocol.cpp /CProtokol line 92 C/C++ Problem
    function definition is not allowed here CProtocol.cpp /CProtokol line 57 C/C++ Problem
    function definition is not allowed here CProtocol.cpp /CProtokol line 73 C/C++ Problem
    function definition is not allowed here CProtocol.cpp /CProtokol line 78 C/C++ Problem

    Man o Man das nervt 😡 😉



  • Du bist oberhalb von getTypeAsString mit den Klammern durcheinandergekommen. Das hat dir deine IDE auch schön mit den Einrückungen angezeigt:

    bool CProtocol::addPayload(char data){
    
          if(m_nextFreePos > MAXPAYLOAD){
                return false;
            }
            else { if (m_nextFreePos <= MAXPAYLOAD){
                m_payload[m_nextFreePos] = data;
                return true;
    
            }
        };
    

    Weiter unten fehlen dann noch ';'



  • Damit wir endlich das Problem lösen können .
    Hier ein screenshot .
    Ich erkenne den Fehler nicht 😕
    https://www.pic-upload.de/view-34329711/Bildschirmfoto2017-11-20um13.15.49.png.html



  • Der if (m_nextFreePos <= MAXPAYLOAD) Kommentar sollte keine Korrektur sein, sondern das else anders formuliert. Sozusagen das Gegenereignis zu if (m_nextFreePos > MAXPAYLOAD) . Wenn ich das mal so direkt sagen darf, Du sollst nicht einfach blind irgendwelche Sachen ersetzen die man sagt, sondern mal selber nachdenken. Ich mache es nochmal deutlicher: Die Fehlerbedingung m_nextFreePos > MAXPAYLOAD ist falsch, denn wenn gilt m_nextFreePos == MAXPAYLOAD ist es auch ein ungültiger Index (eins nach dem letzten Element), und ein > ist nunmal kein >= . D.h. m_nextFreePos muss < MAXPAYLOAD oder wenn m_nextFreePos >= MAXPAYLOAD dann ungültig. Zeile 50 (unter anderem) ist damit also nicht korrigiert. In der Schleife im ctor z.B. machst du es richtig:

    for (int i = 0; i< MAXPAYLOAD; i++)
    // anstatt
    for (int i = 0; i <= MAXPAYLOAD; ++i)  // FALSCH!!!!
    

    Und damit wären wir schon beim nächsten Thema: Nur weil ein C++ Programm kompiliert, bedeutet das noch lange nicht, dass es auch korrekt ist. Also hör bitte auf mit diesem Ziel "Ich möchte dass mein Programm kompiliert". Das ist Grundvoraussetzung, aber noch lange nicht das Endziel.

    P.S.: Du musst Dir auch noch überlegen, wie und wo du m_nextFreePos inkrementierst. Du willst ja nicht bis in alle Ewigkeit auf der ersten Position die Daten reinschreiben. Vermutl. irgendwo in addPayload .

    P.S: Ich hab mir mal die Aufgabe schnell durchgelesen (und das solltest Du am Besten auch nochmal tun), weil mir dein calculateCrc() komisch vorkommt. Und in der Tat, du hast das Summenzeichen komplett ignoriert. Du brauchst definitiv eine for-Schleife innerhalb von calculateCrc().



  • Byson schrieb:

    Damit wir endlich das Problem lösen können .
    Hier ein screenshot .
    Ich erkenne den Fehler nicht 😕
    https://www.pic-upload.de/view-34329711/Bildschirmfoto2017-11-20um13.15.49.png.html

    Bei Fehlermeldungen kann es oft sein, dass der eigentliche Fehler in Zeilen darüber liegt. Die Stelle wurde schon gezeigt. Wenn man sauber einrückt sieht man das sofort.

    bool CProtocol::addPayload(char data)
    {
        if(m_nextFreePos > MAXPAYLOAD){
            return false;
        }
        else { 
            if (m_nextFreePos <= MAXPAYLOAD){
                m_payload[m_nextFreePos] = data;
                return true;
            }
        };
    

    Da fehlt die schließende } für addPayload, bzw. es ist eine '{' zu viel zwischen dem else und dem if.

    P.S.: Ich Empfehle kein Semikolon ans Ende einer Funktionsdefinition zu setzten.


Anmelden zum Antworten