<?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[Rückgabe eines verschachtelten std::vector aus Funktion]]></title><description><![CDATA[<p>Ich habe eine Klasse, die intern Koordinaten mit Hilfe des folgenden Typs <strong>vvvfloat</strong> speichert:</p>
<pre><code class="language-cpp">typedef vector&lt;float&gt; vfloat;
typedef vector&lt;vfloat&gt; vvfloat;
typedef vector&lt;vvfloat&gt; vvvfloat;
</code></pre>
<p>Nun biete ich zum Auslesen folgende Methode an:</p>
<pre><code class="language-cpp">vfloat GetPoint(int x, int y);
</code></pre>
<p>So, wie ich es bisher versucht habe, bekomme ich nur uninitialisierte Werte nach dem Aufruf ausgegeben.</p>
<p>So rufe ich die Funktion auf:</p>
<pre><code class="language-cpp">for (int i = 0; i &lt; pointCount[0]; i++) {
	for (int j = 0; j &lt; pointCount[1]; j++) {
		vfloat point = surface-&gt;GetPoint(i, j);
		cout &lt;&lt; &quot;Point (&quot; &lt;&lt; i &lt;&lt; &quot;, &quot; &lt;&lt; j &lt;&lt; &quot;): &quot; &lt;&lt; point[0] &lt;&lt; &quot;, &quot;
				&lt;&lt; point[1] &lt;&lt; &quot;, &quot; &lt;&lt; point[2] &lt;&lt; endl;
	}
}
</code></pre>
<p>Zur Kontrolle lasse ich in der Funktion selbst ebenfalls die Werte ausgeben, dort werden sie richtig ausgegeben.</p>
<p>Folgende Varianten habe ich bereits versucht, keine funktioniert. Wer kann mir helfen?</p>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	return this-&gt;points[x][y];
}
</code></pre>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	vfloat point = this-&gt;points[x][y];	
	return point;
}
</code></pre>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	vfloat point(this-&gt;points[x][y]);	
	return point;
}
</code></pre>
<p>Danke schon einmal für Eure Hilfe!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/211373/rückgabe-eines-verschachtelten-std-vector-aus-funktion</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 06:26:26 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/211373.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 22 Apr 2008 15:11:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Rückgabe eines verschachtelten std::vector aus Funktion on Tue, 22 Apr 2008 15:12:23 GMT]]></title><description><![CDATA[<p>Ich habe eine Klasse, die intern Koordinaten mit Hilfe des folgenden Typs <strong>vvvfloat</strong> speichert:</p>
<pre><code class="language-cpp">typedef vector&lt;float&gt; vfloat;
typedef vector&lt;vfloat&gt; vvfloat;
typedef vector&lt;vvfloat&gt; vvvfloat;
</code></pre>
<p>Nun biete ich zum Auslesen folgende Methode an:</p>
<pre><code class="language-cpp">vfloat GetPoint(int x, int y);
</code></pre>
<p>So, wie ich es bisher versucht habe, bekomme ich nur uninitialisierte Werte nach dem Aufruf ausgegeben.</p>
<p>So rufe ich die Funktion auf:</p>
<pre><code class="language-cpp">for (int i = 0; i &lt; pointCount[0]; i++) {
	for (int j = 0; j &lt; pointCount[1]; j++) {
		vfloat point = surface-&gt;GetPoint(i, j);
		cout &lt;&lt; &quot;Point (&quot; &lt;&lt; i &lt;&lt; &quot;, &quot; &lt;&lt; j &lt;&lt; &quot;): &quot; &lt;&lt; point[0] &lt;&lt; &quot;, &quot;
				&lt;&lt; point[1] &lt;&lt; &quot;, &quot; &lt;&lt; point[2] &lt;&lt; endl;
	}
}
</code></pre>
<p>Zur Kontrolle lasse ich in der Funktion selbst ebenfalls die Werte ausgeben, dort werden sie richtig ausgegeben.</p>
<p>Folgende Varianten habe ich bereits versucht, keine funktioniert. Wer kann mir helfen?</p>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	return this-&gt;points[x][y];
}
</code></pre>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	vfloat point = this-&gt;points[x][y];	
	return point;
}
</code></pre>
<pre><code class="language-cpp">vfloat SurfaceConfiguration::GetPoint(int x, int y) {
	cout &lt;&lt; &quot;Point (&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;): &quot; &lt;&lt; this-&gt;points[x][y][0] &lt;&lt; &quot;, &quot;
			&lt;&lt; this-&gt;points[x][y][1] &lt;&lt; &quot;, &quot; &lt;&lt; this-&gt;points[x][y][2] &lt;&lt; endl;
	vfloat point(this-&gt;points[x][y]);	
	return point;
}
</code></pre>
<p>Danke schon einmal für Eure Hilfe!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496265</guid><dc:creator><![CDATA[Chewie83]]></dc:creator><pubDate>Tue, 22 Apr 2008 15:12:23 GMT</pubDate></item><item><title><![CDATA[Reply to Rückgabe eines verschachtelten std::vector aus Funktion on Tue, 22 Apr 2008 15:19:14 GMT]]></title><description><![CDATA[<p>Wie füllst Du denn die Vektoren bzw. mit welcher Größe initialisierst Du sie?<br />
Probier mal das:</p>
<pre><code class="language-cpp">return points.at(x).at(y);
</code></pre>
<p>Wenn Du da eine Exception bekommst, dann ist Dein Vektor für die Idizes nichtmal initialisiert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496271</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496271</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 22 Apr 2008 15:19:14 GMT</pubDate></item><item><title><![CDATA[Reply to Rückgabe eines verschachtelten std::vector aus Funktion on Tue, 22 Apr 2008 15:23:50 GMT]]></title><description><![CDATA[<p>Die Werte werden aus einer Datei gelesen, in der pro Zeile drei Zahlen stehen, je durch ein Komma getrennt.</p>
<p>Sollte im Prinzip aber irrelevant sein, da die Ausgabe der Punkte innerhalb der Klasse ja richtig funktioniert.</p>
<p>Der Vollständigkeit halber hier der Code:</p>
<pre><code class="language-cpp">void SurfaceConfiguration::loadPoints() {
	ifstream pointsFile(&quot;points.gen&quot;);
	char buffer[100]; // just to make sure the whole line fits in

	int length = pointCount[0];
	int width = pointCount[1];
	points.resize(length);
	for (int i = 0; i &lt; length; i++) {
		this-&gt;points[i].resize(width);
		for (int j = 0; j &lt; width; j++) {
			points[i].resize(width);

			// read and split line
			pointsFile.getline(buffer, sizeof(buffer));
			vector&lt;string&gt; thisPoint = explode(buffer, &quot;,&quot;);

			for (int k = 0; k &lt;= 2; k++) {
				istringstream(thisPoint[k]) &gt;&gt; this-&gt;points[i][j][k];
				//cout &lt;&lt; &quot;Point (&quot; &lt;&lt; i &lt;&lt; &quot;, &quot; &lt;&lt; j &lt;&lt; &quot;)[&quot; &lt;&lt; k &lt;&lt; &quot;]: &quot; &lt;&lt; this-&gt;points[i][j][k] &lt;&lt; endl;
			}
		}
	}
}
</code></pre>
<p>Edit:</p>
<pre><code class="language-cpp">return points.at(x).at(y);
</code></pre>
<p>ist doch das gleiche wie</p>
<pre><code class="language-cpp">return points.[x][y];
</code></pre>
<p>oder nicht?</p>
<p>Das Ergebnis unterscheidet sich jedenfalls nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496273</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496273</guid><dc:creator><![CDATA[Chewie83]]></dc:creator><pubDate>Tue, 22 Apr 2008 15:23:50 GMT</pubDate></item><item><title><![CDATA[Reply to Rückgabe eines verschachtelten std::vector aus Funktion on Tue, 22 Apr 2008 15:47:30 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">vector.at(index)
</code></pre>
<p>Prüft seine Grenzen und wirft eine Exception, wenn man versucht über sie hinaus zu lesen.</p>
<pre><code class="language-cpp">vector.operator[](index)
</code></pre>
<p>Prüft hingegen seine Grenzen nicht und erlaubt so, auf nicht allokierten Speicher zuzugreifen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496283</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Tue, 22 Apr 2008 15:47:30 GMT</pubDate></item><item><title><![CDATA[Reply to Rückgabe eines verschachtelten std::vector aus Funktion on Wed, 23 Apr 2008 09:58:17 GMT]]></title><description><![CDATA[<p>Argh, ich bin doof!!</p>
<p>Zeile 11 macht den Unterschied:</p>
<p>Falsch:</p>
<pre><code class="language-cpp">void SurfaceConfiguration::loadPoints() {
    ifstream pointsFile(&quot;points.gen&quot;);
    char buffer[100]; // just to make sure the whole line fits in

    int length = pointCount[0];
    int width = pointCount[1];
    points.resize(length);
    for (int i = 0; i &lt; length; i++) {
        this-&gt;points[i].resize(width);
        for (int j = 0; j &lt; width; j++) {
            points[i].resize(width);

            // read and split line
            pointsFile.getline(buffer, sizeof(buffer));
            vector&lt;string&gt; thisPoint = explode(buffer, &quot;,&quot;);

            for (int k = 0; k &lt;= 2; k++) {
                istringstream(thisPoint[k]) &gt;&gt; this-&gt;points[i][j][k];
                //cout &lt;&lt; &quot;Point (&quot; &lt;&lt; i &lt;&lt; &quot;, &quot; &lt;&lt; j &lt;&lt; &quot;)[&quot; &lt;&lt; k &lt;&lt; &quot;]: &quot; &lt;&lt; this-&gt;points[i][j][k] &lt;&lt; endl;
            }
        }
    }
}
</code></pre>
<p>Richtig:</p>
<pre><code class="language-cpp">void SurfaceConfiguration::loadPoints() {
	ifstream pointsFile(&quot;points.gen&quot;);
	char buffer[100]; // just to make sure the whole line fits in

	int length = pointCount[0];
	int width = pointCount[1];
	points.resize(length);
	for (int i = 0; i &lt; length; i++) {
		this-&gt;points[i].resize(width);
		for (int j = 0; j &lt; width; j++) {
			this-&gt;points[i][j].resize(3);

			// read and split line
			pointsFile.getline(buffer, sizeof(buffer));
			vector&lt;string&gt; thisPoint = explode(buffer, &quot;,&quot;);

			for (int k = 0; k &lt;= 2; k++) {
				istringstream(thisPoint[k]) &gt;&gt; this-&gt;points[i][j][k];
			}
		}
	}
}
</code></pre>
<p>Hätte ich eigentlich eher sehen müssen, aber naja, jedenfalls dennoch danke, tachyon, für die Belehrung!</p>
<p>Problem gelöst!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1496711</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1496711</guid><dc:creator><![CDATA[Chewie83]]></dc:creator><pubDate>Wed, 23 Apr 2008 09:58:17 GMT</pubDate></item></channel></rss>