<?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[struct an funktion übergeben in c++]]></title><description><![CDATA[<p>Hallo an alle,</p>
<p>ich hoffe ich bin im richtigen Forums-Bereich, wenn nicht dann bitte mal verschieben.<br />
Hab hier ein kleines Problem. Ich möchte eine Funktion schreiben die mir Dateien erstellt mit Daten die aus einem Struct-Konstrukt kommen.</p>
<p>Schreib ich den Code alles in eine Quelldatei funktioniert das auch tadellos.</p>
<p>Hier der Quelltext wie es funktioniert, entschuldigt die eventuelle &quot;Unordnung&quot; hab das jetzt nur schnell zusammenkopiert.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
using namespace std;

int main()
{
	typedef double	coord_double;
	typedef struct  
	{ coord_double x;
	  coord_double y;
	  coord_double z;
	} coord_anchor;

	int anchor_n=4;

	coord_anchor anchor[4];

	anchor[1].x=1;
	anchor[1].y=2;
	anchor[1].z=3;
	anchor[2].x=4;
	anchor[2].y=5;
	anchor[2].z=6;
        //...

	for (int i=1;i&lt;=anchor_n;i++)
	{
	stringstream str;
	str &lt;&lt; &quot;anchor&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;	
	ofstream ofs ( str.str().c_str()); 
    if (!ofs.bad())
		{
        ofs &lt;&lt;
		&quot;Anchor &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;
		&quot;x &quot;&lt;&lt;anchor[i].x&lt;&lt;&quot;, y &quot;&lt;&lt;anchor[i].y&lt;&lt;&quot;, z &quot;&lt;&lt;anchor[i].z&lt;&lt;&quot;\n&quot;&lt;&lt; endl;
        ofs.close();
	    }
	}
}
</code></pre>
<p>Schreibe ich jetzt aber den Algorithmus zur Dateiausgabe in eine extra Datei und deklarier das ganze als Funktion die die Daten übergeben bekommt funktioniert nix mehr. Hier mal so wie ich es aufgeteilt hatte.</p>
<p>Header-Files:</p>
<p>coord.h</p>
<pre><code class="language-cpp">#ifndef COORD_H
#define COORD_H

typedef double	coord_double;
	typedef struct  
	{	coord_double x;
		coord_double y;
		coord_double z;
	} coord_anchor;
#endif
</code></pre>
<p>file.h</p>
<pre><code class="language-cpp">#ifndef FILE_H
#define FILE_H
void file_out(int,coord_anchor);
#endif
</code></pre>
<p>Hier die c++ Datein<br />
file_out.cpp</p>
<pre><code class="language-cpp">#include &lt;sstream&gt;
#include &lt;fstream&gt;

using namespace std;

void file_out(int anchor_n,double anchor[])
{
for (int i=1;i&lt;=anchor_n;i++)
	{
	stringstream str;
	str &lt;&lt; &quot;anchor&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;	
	ofstream ofs ( str.str().c_str()); 
    if (!ofs.bad())
     {
        ofs &lt;&lt;
	    &quot;Anchor &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;
	    &quot;x &quot;&lt;&lt;anchor[i].x&lt;&lt;&quot;, y &quot;&lt;&lt;anchor[i].y&lt;&lt;&quot;, z &quot;&lt;&lt;anchor[i].z&lt;&lt;&quot;\n&quot;&lt;&lt; endl;
        ofs.close();
     }
	}
}
</code></pre>
<p>test.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
#include &quot;coord.h&quot;
#include &quot;file.h&quot;
using namespace std;

int main()
{	
	int anchor_n=4;
	coord_anchor anchor[4];
	anchor[1].x=1;
	anchor[1].y=2;
	anchor[1].z=3;
	anchor[2].x=4;
	anchor[2].y=5;
	anchor[2].z=6;

	file_out(anchor_n, anchor[4]);
	return 0;	

}
</code></pre>
<p>Da bekomme ich beim Kompilieren selber keine Fehler, erst wenn ich das Debugging starte bekomme ich folgende Fehlermeldungen und ich weiss einfach nicht mehr weiter.</p>
<pre><code class="language-cpp">------ Erstellen gestartet: Projekt: test, Konfiguration: Debug Win32 ------
Kompilieren...
file_out.cpp
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.x&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.y&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.z&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
Das Buildprotokoll wurde unter &quot;file://d:\eigene Dateien\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm&quot; gespeichert.
test - 3 Fehler, 0 Warnung(en)
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========
</code></pre>
<p>Falls sich jemand der Sache annimmt und Rat weiss dann danke ich schon mal im Voraus. Ich sollte eventuell erwähnen das ich Visual C++ 2008 nutze und eigentlich blutiger Anfänger bin.</p>
<p>mfg Morxi</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/231465/struct-an-funktion-übergeben-in-c</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 20:53:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/231465.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 11 Jan 2009 11:24:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to struct an funktion übergeben in c++ on Sun, 11 Jan 2009 14:22:04 GMT]]></title><description><![CDATA[<p>Hallo an alle,</p>
<p>ich hoffe ich bin im richtigen Forums-Bereich, wenn nicht dann bitte mal verschieben.<br />
Hab hier ein kleines Problem. Ich möchte eine Funktion schreiben die mir Dateien erstellt mit Daten die aus einem Struct-Konstrukt kommen.</p>
<p>Schreib ich den Code alles in eine Quelldatei funktioniert das auch tadellos.</p>
<p>Hier der Quelltext wie es funktioniert, entschuldigt die eventuelle &quot;Unordnung&quot; hab das jetzt nur schnell zusammenkopiert.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
using namespace std;

int main()
{
	typedef double	coord_double;
	typedef struct  
	{ coord_double x;
	  coord_double y;
	  coord_double z;
	} coord_anchor;

	int anchor_n=4;

	coord_anchor anchor[4];

	anchor[1].x=1;
	anchor[1].y=2;
	anchor[1].z=3;
	anchor[2].x=4;
	anchor[2].y=5;
	anchor[2].z=6;
        //...

	for (int i=1;i&lt;=anchor_n;i++)
	{
	stringstream str;
	str &lt;&lt; &quot;anchor&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;	
	ofstream ofs ( str.str().c_str()); 
    if (!ofs.bad())
		{
        ofs &lt;&lt;
		&quot;Anchor &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;
		&quot;x &quot;&lt;&lt;anchor[i].x&lt;&lt;&quot;, y &quot;&lt;&lt;anchor[i].y&lt;&lt;&quot;, z &quot;&lt;&lt;anchor[i].z&lt;&lt;&quot;\n&quot;&lt;&lt; endl;
        ofs.close();
	    }
	}
}
</code></pre>
<p>Schreibe ich jetzt aber den Algorithmus zur Dateiausgabe in eine extra Datei und deklarier das ganze als Funktion die die Daten übergeben bekommt funktioniert nix mehr. Hier mal so wie ich es aufgeteilt hatte.</p>
<p>Header-Files:</p>
<p>coord.h</p>
<pre><code class="language-cpp">#ifndef COORD_H
#define COORD_H

typedef double	coord_double;
	typedef struct  
	{	coord_double x;
		coord_double y;
		coord_double z;
	} coord_anchor;
#endif
</code></pre>
<p>file.h</p>
<pre><code class="language-cpp">#ifndef FILE_H
#define FILE_H
void file_out(int,coord_anchor);
#endif
</code></pre>
<p>Hier die c++ Datein<br />
file_out.cpp</p>
<pre><code class="language-cpp">#include &lt;sstream&gt;
#include &lt;fstream&gt;

using namespace std;

void file_out(int anchor_n,double anchor[])
{
for (int i=1;i&lt;=anchor_n;i++)
	{
	stringstream str;
	str &lt;&lt; &quot;anchor&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;	
	ofstream ofs ( str.str().c_str()); 
    if (!ofs.bad())
     {
        ofs &lt;&lt;
	    &quot;Anchor &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot;
	    &quot;x &quot;&lt;&lt;anchor[i].x&lt;&lt;&quot;, y &quot;&lt;&lt;anchor[i].y&lt;&lt;&quot;, z &quot;&lt;&lt;anchor[i].z&lt;&lt;&quot;\n&quot;&lt;&lt; endl;
        ofs.close();
     }
	}
}
</code></pre>
<p>test.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;fstream&gt;
#include &quot;coord.h&quot;
#include &quot;file.h&quot;
using namespace std;

int main()
{	
	int anchor_n=4;
	coord_anchor anchor[4];
	anchor[1].x=1;
	anchor[1].y=2;
	anchor[1].z=3;
	anchor[2].x=4;
	anchor[2].y=5;
	anchor[2].z=6;

	file_out(anchor_n, anchor[4]);
	return 0;	

}
</code></pre>
<p>Da bekomme ich beim Kompilieren selber keine Fehler, erst wenn ich das Debugging starte bekomme ich folgende Fehlermeldungen und ich weiss einfach nicht mehr weiter.</p>
<pre><code class="language-cpp">------ Erstellen gestartet: Projekt: test, Konfiguration: Debug Win32 ------
Kompilieren...
file_out.cpp
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.x&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.y&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
d:\eigene dateien\visual studio 2008\projects\test\test\file_out.cpp(18) : error C2228: Links von &quot;.z&quot; muss sich eine Klasse/Struktur/Union befinden.
        Typ ist 'double'
Das Buildprotokoll wurde unter &quot;file://d:\eigene Dateien\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm&quot; gespeichert.
test - 3 Fehler, 0 Warnung(en)
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========
</code></pre>
<p>Falls sich jemand der Sache annimmt und Rat weiss dann danke ich schon mal im Voraus. Ich sollte eventuell erwähnen das ich Visual C++ 2008 nutze und eigentlich blutiger Anfänger bin.</p>
<p>mfg Morxi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1643287</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1643287</guid><dc:creator><![CDATA[Morxi]]></dc:creator><pubDate>Sun, 11 Jan 2009 14:22:04 GMT</pubDate></item><item><title><![CDATA[Reply to struct an funktion übergeben in c++ on Mon, 12 Jan 2009 07:18:41 GMT]]></title><description><![CDATA[<blockquote>
<pre><code class="language-cpp">void file_out(int anchor_n,double anchor[]) 
{ 
for (int i=1;i&lt;=anchor_n;i++) 
    { 
    stringstream str; 
    str &lt;&lt; &quot;anchor&quot; &lt;&lt; i &lt;&lt; &quot;.txt&quot;;    
    ofstream ofs ( str.str().c_str()); 
    if (!ofs.bad()) 
     { 
        ofs &lt;&lt; 
        &quot;Anchor &quot;&lt;&lt;i&lt;&lt;&quot;\n&quot; 
        &quot;x &quot;&lt;&lt;anchor[i].x&lt;&lt;&quot;, y &quot;&lt;&lt;anchor[i].y&lt;&lt;&quot;, z &quot;&lt;&lt;anchor[i].z&lt;&lt;&quot;\n&quot;&lt;&lt; endl; 
        ofs.close(); 
     } 
    } 
}
</code></pre>
</blockquote>
<p>Du notierst den Parameter anchor als double-Array, nicht als dein struct. double ist nunmal keine Klasse mit den Elementen x, y und z. Das meckert der Compiler an. Du solltest dort also deinen Typen struct coord_anchor angeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1643718</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1643718</guid><dc:creator><![CDATA[_matze]]></dc:creator><pubDate>Mon, 12 Jan 2009 07:18:41 GMT</pubDate></item><item><title><![CDATA[Reply to struct an funktion übergeben in c++ on Mon, 12 Jan 2009 11:36:53 GMT]]></title><description><![CDATA[<p>Problem gelöst, danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1643812</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1643812</guid><dc:creator><![CDATA[Morxi]]></dc:creator><pubDate>Mon, 12 Jan 2009 11:36:53 GMT</pubDate></item><item><title><![CDATA[Reply to struct an funktion übergeben in c++ on Mon, 12 Jan 2009 12:04:19 GMT]]></title><description><![CDATA[<p>Dieser Thread wurde von Moderator/in <a href="http://www.c-plusplus.net/forum/profile-var-mode-is-viewprofile-and-u-is-18363.html" rel="nofollow">Jochen Kalmbach</a> aus dem Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-58.html" rel="nofollow">C++/CLI mit .NET</a> in das Forum <a href="http://www.c-plusplus.net/forum/viewforum-var-f-is-15.html" rel="nofollow">C++</a> verschoben.</p>
<p>Im Zweifelsfall bitte auch folgende Hinweise beachten:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-39405.html" rel="nofollow">C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?</a></p>
<p><em>Dieses Posting wurde automatisch erzeugt.</em></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1643827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1643827</guid><dc:creator><![CDATA[C++ Forumbot]]></dc:creator><pubDate>Mon, 12 Jan 2009 12:04:19 GMT</pubDate></item><item><title><![CDATA[Reply to struct an funktion übergeben in c++ on Mon, 12 Jan 2009 12:25:50 GMT]]></title><description><![CDATA[<p>Morxi schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Noch ein paar kleine Anmerkungen.</p>
<p>1. Auch wenn man hier vielleicht die Bedeutung leicht erkennt, rate ich dir Abkürzungen für Variablen ab (nach einen halben Jahr hat man sonst manchmal Verständnisprobleme). Und vor allem Rate ich grundsätzlich zu sprechenden Namen.<br />
2. typedef struct ist ein altes C-Relikt. enum, struct, class... sind bereits Typdefinitionen.</p>
<pre><code class="language-cpp">// In C++ unüblich:
typedef struct  
{ coord_double x;
  coord_double y;
  coord_double z;
} coord_anchor;

// In C++ üblich:
struct coord_anchor
{ coord_double x;
  coord_double y;
  coord_double z;
};
</code></pre>
<p>3. In C++ würde ich auch in Strukturen Konstruktoren nutzen, spart häufig einiges bei der Verwendung.<br />
4. Häufig bessere Alternativen zum C-Array sind std::tr1::array (Falls der TR1 unter deinem Compiler unterstützt wird) oder std::vector.<br />
5. Ich würde auch in Headern immer die Parameternamen mit Angeben.<br />
Warum? Man sollte immer gegen die Schnittstelle, nie die Implementierung programmieren. In dem man zum Verständnis aber gezwungen ist in die Implementierung zu schauen, wird mit diesem Prinzip schon gebrochen.</p>
<p>cu André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1643839</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1643839</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Mon, 12 Jan 2009 12:25:50 GMT</pubDate></item></channel></rss>