<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[C++ Referenzenfehler in Verbindung mit jsoncons]]></title><description><![CDATA[<p>Hallo Community,</p>
<p>ich habe irgendwie ein Denkfehler im folgenden Quellcode:</p>
<pre><code>int asJsonString(std::string&amp; out) {
            int action = 0;
            int errors = 0;

            json root;
            json *base = &amp;root;

            DataObject *lastDataObject;
            DataObject *currentDataObject;

            currentDataObject = &amp;this-&gt;dataobject;

            // make as array ...
            if (currentDataObject-&gt;object.get()-&gt;type == ARRAY) {
                root = json::make_array();
            }

            while(running) { //correct
                action = 0;
                currentDataObject-&gt;each([this, &amp;lastDataObject, &amp;currentDataObject, &amp;out, &amp;errors, &amp;action](Object const&amp; obj) {

                    if (obj.get()-&gt;type != NONE) {
                        if ((obj.get()-&gt;type == DICT_ELEMENT) || (obj.get()-&gt;type == ARRAY_ELEMENT)) {
                            int error = 0;

                            std::string value = DataConverter(obj).toString(error);
                            //std::cout &lt;&lt; value &lt;&lt; std::endl;

                            //if (obj.get()-&gt;type == ARRAY_ELEMENT) {
                            //    
                            //    base-&gt;add()
                            //}

                            //if (obj.get()-&gt;type == DICT_ELEMENT) {
                            //    
                            //}

                            errors += error;

                            action = 1;
                        }
                        else {
                            if (DataHolder&lt;std::shared_ptr&lt;DataObject&gt;&gt; *dataholder_ptr = dynamic_cast&lt;DataHolder&lt;std::shared_ptr&lt;DataObject&gt;&gt;*&gt;(obj.get())) {
                                DataObject * dataobject = dataholder_ptr-&gt;value.get();
                                //std::cout &lt;&lt; dataobject-&gt;object.get()-&gt;key &lt;&lt; std::endl;
                                lastDataObject = currentDataObject;
                                currentDataObject = dataobject; // go to next dataobject

                                action = 1;
                            }        
                        }
                    }
            	});

            	if (!action) {
            	    break;
            	}
            	else {
            	    json newbase;

            	    std::cout &lt;&lt; currentDataObject-&gt;object.get()-&gt;key;

            	    if (currentDataObject-&gt;object.get()-&gt;type == ARRAY) {
            	        std::cout &lt;&lt; &quot;MKARRAY&quot; &lt;&lt; std::endl;
            	        newbase = json::make_array();
            	    }

            	    if (lastDataObject-&gt;object.get()-&gt;type == ARRAY) {
            	        std::cout &lt;&lt; &quot;ARRAY&quot; &lt;&lt; std::endl;
            	        base-&gt;add(std::move(newbase));
            	    }

            	    if (lastDataObject-&gt;object.get()-&gt;type == DICT) {
            	        std::cout &lt;&lt; &quot;DICT&quot; &lt;&lt; std::endl;
            	        base-&gt;set(currentDataObject-&gt;object.get()-&gt;key, std::move(newbase));
            	        //base-&gt;set(currentDataObject-&gt;object.get()-&gt;key, json::null());
            	    }
            	    base = &amp;newbase;
            	    std::cout &lt;&lt; root &lt;&lt; std::endl;

                }
            }
            return errors;
        }
</code></pre>
<p>Das each-loop ist über folgendes:</p>
<pre><code>unsigned const int getSize() {
                return objectVectorHolder-&gt;value.size();
            }

            void setIndex(int index) {
                this-&gt;index = index;    
            }

            unsigned const int getIndex() {
                return this-&gt;index;
            }

            Object const&amp; operator++(int) {
                this-&gt;index++;
                return this-&gt;get(); 
            }

            Object const&amp; operator--(int) {
                this-&gt;index--;
                return this-&gt;get(); 
            }

            Object const&amp; get() {
                return objectVectorHolder-&gt;value.at(this-&gt;index); 
            }

            Object const&amp; get(int &amp;error) {
                error = 1;

                if (this-&gt;index &lt; objectVectorHolder-&gt;value.size()) {
                    error = 0;
                    return objectVectorHolder-&gt;value.at(this-&gt;index); 
                }

                return objectVectorHolder-&gt;value.at(objectVectorHolder-&gt;value.size()-1); 
            }

            void each(std::function&lt;void(Object const&amp;)&gt; fn) {
                this-&gt;index = 0;

                if (objectVectorHolder-&gt;value.size() != 0) {
                    while(1) { // correct
                        if (this-&gt;index &lt; objectVectorHolder-&gt;value.size()) {
                            fn(this-&gt;get());

                            this-&gt;index++;
                        }
                        else {
                            break;
                        }
                    }
                }
        	}
</code></pre>
<p>Dabei erhalte ich folgende Ausgabe:</p>
<pre><code>testARRAY
[{}]
subtree1DICT
[{}]
aMKARRAY
DICT
</code></pre>
<p>Die Struktur sieht aber wie folgt aus:</p>
<pre><code>a : [
    (string) 1,
    (string) 2,
    (string) 3,
    (string) 4,
    test : {
        5: (string) 5,
        subtree1 : {
            6: (string) 6,
            7: (string) 7,
            a : []
            }
        }
    ]
</code></pre>
<p>Daraus müsste sich beim JSON ergeben:</p>
<p>(a)[{{[]}}] - sich ergeben. Das a wird ignoriert, das stellt das Array dar.</p>
<p>Was habe ich falsch gemacht? Wo liegt mein Denkfehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/339797/c-referenzenfehler-in-verbindung-mit-jsoncons</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 21:24:31 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/339797.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Sep 2016 05:45:22 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ Referenzenfehler in Verbindung mit jsoncons on Thu, 29 Sep 2016 05:45:22 GMT]]></title><description><![CDATA[<p>Hallo Community,</p>
<p>ich habe irgendwie ein Denkfehler im folgenden Quellcode:</p>
<pre><code>int asJsonString(std::string&amp; out) {
            int action = 0;
            int errors = 0;

            json root;
            json *base = &amp;root;

            DataObject *lastDataObject;
            DataObject *currentDataObject;

            currentDataObject = &amp;this-&gt;dataobject;

            // make as array ...
            if (currentDataObject-&gt;object.get()-&gt;type == ARRAY) {
                root = json::make_array();
            }

            while(running) { //correct
                action = 0;
                currentDataObject-&gt;each([this, &amp;lastDataObject, &amp;currentDataObject, &amp;out, &amp;errors, &amp;action](Object const&amp; obj) {

                    if (obj.get()-&gt;type != NONE) {
                        if ((obj.get()-&gt;type == DICT_ELEMENT) || (obj.get()-&gt;type == ARRAY_ELEMENT)) {
                            int error = 0;

                            std::string value = DataConverter(obj).toString(error);
                            //std::cout &lt;&lt; value &lt;&lt; std::endl;

                            //if (obj.get()-&gt;type == ARRAY_ELEMENT) {
                            //    
                            //    base-&gt;add()
                            //}

                            //if (obj.get()-&gt;type == DICT_ELEMENT) {
                            //    
                            //}

                            errors += error;

                            action = 1;
                        }
                        else {
                            if (DataHolder&lt;std::shared_ptr&lt;DataObject&gt;&gt; *dataholder_ptr = dynamic_cast&lt;DataHolder&lt;std::shared_ptr&lt;DataObject&gt;&gt;*&gt;(obj.get())) {
                                DataObject * dataobject = dataholder_ptr-&gt;value.get();
                                //std::cout &lt;&lt; dataobject-&gt;object.get()-&gt;key &lt;&lt; std::endl;
                                lastDataObject = currentDataObject;
                                currentDataObject = dataobject; // go to next dataobject

                                action = 1;
                            }        
                        }
                    }
            	});

            	if (!action) {
            	    break;
            	}
            	else {
            	    json newbase;

            	    std::cout &lt;&lt; currentDataObject-&gt;object.get()-&gt;key;

            	    if (currentDataObject-&gt;object.get()-&gt;type == ARRAY) {
            	        std::cout &lt;&lt; &quot;MKARRAY&quot; &lt;&lt; std::endl;
            	        newbase = json::make_array();
            	    }

            	    if (lastDataObject-&gt;object.get()-&gt;type == ARRAY) {
            	        std::cout &lt;&lt; &quot;ARRAY&quot; &lt;&lt; std::endl;
            	        base-&gt;add(std::move(newbase));
            	    }

            	    if (lastDataObject-&gt;object.get()-&gt;type == DICT) {
            	        std::cout &lt;&lt; &quot;DICT&quot; &lt;&lt; std::endl;
            	        base-&gt;set(currentDataObject-&gt;object.get()-&gt;key, std::move(newbase));
            	        //base-&gt;set(currentDataObject-&gt;object.get()-&gt;key, json::null());
            	    }
            	    base = &amp;newbase;
            	    std::cout &lt;&lt; root &lt;&lt; std::endl;

                }
            }
            return errors;
        }
</code></pre>
<p>Das each-loop ist über folgendes:</p>
<pre><code>unsigned const int getSize() {
                return objectVectorHolder-&gt;value.size();
            }

            void setIndex(int index) {
                this-&gt;index = index;    
            }

            unsigned const int getIndex() {
                return this-&gt;index;
            }

            Object const&amp; operator++(int) {
                this-&gt;index++;
                return this-&gt;get(); 
            }

            Object const&amp; operator--(int) {
                this-&gt;index--;
                return this-&gt;get(); 
            }

            Object const&amp; get() {
                return objectVectorHolder-&gt;value.at(this-&gt;index); 
            }

            Object const&amp; get(int &amp;error) {
                error = 1;

                if (this-&gt;index &lt; objectVectorHolder-&gt;value.size()) {
                    error = 0;
                    return objectVectorHolder-&gt;value.at(this-&gt;index); 
                }

                return objectVectorHolder-&gt;value.at(objectVectorHolder-&gt;value.size()-1); 
            }

            void each(std::function&lt;void(Object const&amp;)&gt; fn) {
                this-&gt;index = 0;

                if (objectVectorHolder-&gt;value.size() != 0) {
                    while(1) { // correct
                        if (this-&gt;index &lt; objectVectorHolder-&gt;value.size()) {
                            fn(this-&gt;get());

                            this-&gt;index++;
                        }
                        else {
                            break;
                        }
                    }
                }
        	}
</code></pre>
<p>Dabei erhalte ich folgende Ausgabe:</p>
<pre><code>testARRAY
[{}]
subtree1DICT
[{}]
aMKARRAY
DICT
</code></pre>
<p>Die Struktur sieht aber wie folgt aus:</p>
<pre><code>a : [
    (string) 1,
    (string) 2,
    (string) 3,
    (string) 4,
    test : {
        5: (string) 5,
        subtree1 : {
            6: (string) 6,
            7: (string) 7,
            a : []
            }
        }
    ]
</code></pre>
<p>Daraus müsste sich beim JSON ergeben:</p>
<p>(a)[{{[]}}] - sich ergeben. Das a wird ignoriert, das stellt das Array dar.</p>
<p>Was habe ich falsch gemacht? Wo liegt mein Denkfehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2510075</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510075</guid><dc:creator><![CDATA[Jonas15]]></dc:creator><pubDate>Thu, 29 Sep 2016 05:45:22 GMT</pubDate></item><item><title><![CDATA[Reply to C++ Referenzenfehler in Verbindung mit jsoncons on Thu, 29 Sep 2016 10:01:10 GMT]]></title><description><![CDATA[<p>Jonas15 schrieb:</p>
<blockquote>
<p>Was habe ich falsch gemacht? Wo liegt mein Denkfehler.</p>
</blockquote>
<p>Du hast angenommen, dass dir jemand das Programm debugged. Und dabei ist es noch nicht einmal übersetzbar.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2510104</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2510104</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Thu, 29 Sep 2016 10:01:10 GMT</pubDate></item></channel></rss>