C++ Referenzenfehler in Verbindung mit jsoncons



  • Hallo Community,

    ich habe irgendwie ein Denkfehler im folgenden Quellcode:

    int asJsonString(std::string& out) {
                int action = 0;
                int errors = 0;
    
                json root;
                json *base = &root;
    
                DataObject *lastDataObject;
                DataObject *currentDataObject;
    
                currentDataObject = &this->dataobject;
    
                // make as array ...
                if (currentDataObject->object.get()->type == ARRAY) {
                    root = json::make_array();
                }
    
                while(running) { //correct
                    action = 0;
                    currentDataObject->each([this, &lastDataObject, &currentDataObject, &out, &errors, &action](Object const& obj) {
    
                        if (obj.get()->type != NONE) {
                            if ((obj.get()->type == DICT_ELEMENT) || (obj.get()->type == ARRAY_ELEMENT)) {
                                int error = 0;
    
                                std::string value = DataConverter(obj).toString(error);
                                //std::cout << value << std::endl;
    
                                //if (obj.get()->type == ARRAY_ELEMENT) {
                                //    
                                //    base->add()
                                //}
    
                                //if (obj.get()->type == DICT_ELEMENT) {
                                //    
                                //}
    
                                errors += error;
    
                                action = 1;
                            }
                            else {
                                if (DataHolder<std::shared_ptr<DataObject>> *dataholder_ptr = dynamic_cast<DataHolder<std::shared_ptr<DataObject>>*>(obj.get())) {
                                    DataObject * dataobject = dataholder_ptr->value.get();
                                    //std::cout << dataobject->object.get()->key << std::endl;
                                    lastDataObject = currentDataObject;
                                    currentDataObject = dataobject; // go to next dataobject
    
                                    action = 1;
                                }        
                            }
                        }
                	});
    
                	if (!action) {
                	    break;
                	}
                	else {
                	    json newbase;
    
                	    std::cout << currentDataObject->object.get()->key;
    
                	    if (currentDataObject->object.get()->type == ARRAY) {
                	        std::cout << "MKARRAY" << std::endl;
                	        newbase = json::make_array();
                	    }
    
                	    if (lastDataObject->object.get()->type == ARRAY) {
                	        std::cout << "ARRAY" << std::endl;
                	        base->add(std::move(newbase));
                	    }
    
                	    if (lastDataObject->object.get()->type == DICT) {
                	        std::cout << "DICT" << std::endl;
                	        base->set(currentDataObject->object.get()->key, std::move(newbase));
                	        //base->set(currentDataObject->object.get()->key, json::null());
                	    }
                	    base = &newbase;
                	    std::cout << root << std::endl;
    
                    }
                }
                return errors;
            }
    

    Das each-loop ist über folgendes:

    unsigned const int getSize() {
                    return objectVectorHolder->value.size();
                }
    
                void setIndex(int index) {
                    this->index = index;    
                }
    
                unsigned const int getIndex() {
                    return this->index;
                }
    
                Object const& operator++(int) {
                    this->index++;
                    return this->get(); 
                }
    
                Object const& operator--(int) {
                    this->index--;
                    return this->get(); 
                }
    
                Object const& get() {
                    return objectVectorHolder->value.at(this->index); 
                }
    
                Object const& get(int &error) {
                    error = 1;
    
                    if (this->index < objectVectorHolder->value.size()) {
                        error = 0;
                        return objectVectorHolder->value.at(this->index); 
                    }
    
                    return objectVectorHolder->value.at(objectVectorHolder->value.size()-1); 
                }
    
                void each(std::function<void(Object const&)> fn) {
                    this->index = 0;
    
                    if (objectVectorHolder->value.size() != 0) {
                        while(1) { // correct
                            if (this->index < objectVectorHolder->value.size()) {
                                fn(this->get());
    
                                this->index++;
                            }
                            else {
                                break;
                            }
                        }
                    }
            	}
    

    Dabei erhalte ich folgende Ausgabe:

    testARRAY
    [{}]
    subtree1DICT
    [{}]
    aMKARRAY
    DICT
    

    Die Struktur sieht aber wie folgt aus:

    a : [
        (string) 1,
        (string) 2,
        (string) 3,
        (string) 4,
        test : {
            5: (string) 5,
            subtree1 : {
                6: (string) 6,
                7: (string) 7,
                a : []
                }
            }
        ]
    

    Daraus müsste sich beim JSON ergeben:

    (a)[{{[]}}] - sich ergeben. Das a wird ignoriert, das stellt das Array dar.

    Was habe ich falsch gemacht? Wo liegt mein Denkfehler.



  • Jonas15 schrieb:

    Was habe ich falsch gemacht? Wo liegt mein Denkfehler.

    Du hast angenommen, dass dir jemand das Programm debugged. Und dabei ist es noch nicht einmal übersetzbar.


Anmelden zum Antworten