<?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[Anfängerfrage]]></title><description><![CDATA[<p>Ich habe ein ziemliche Anfängerfrage, habe dazu aber irgendwie nichts gefunden, weil ich nicht weiß wonach ich suchen soll.<br />
Und zwar würde ich gerne wissen, wie ich eine Variable aus test1.cpp in test2.cpp bekomme.<br />
Das sollte also ungefähr so aussehen:</p>
<pre><code class="language-cpp">// Test1.cpp

int hoehe = 5;
</code></pre>
<pre><code class="language-cpp">// Test2.cpp

int breite = 5;
int flaeche = breite * hoehe;
</code></pre>
<p>Wenn ich die Variable hoehe in test1.cpp ganz einfach global initialisiere, kann ich sie aber nicht in test2.cpp nutzen. Nun möchte ich eben gerne wissen wie das geht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/271031/anfängerfrage</link><generator>RSS for Node</generator><lastBuildDate>Sat, 29 Aug 2026 17:04:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/271031.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 22 Jul 2010 11:01:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 11:01:00 GMT]]></title><description><![CDATA[<p>Ich habe ein ziemliche Anfängerfrage, habe dazu aber irgendwie nichts gefunden, weil ich nicht weiß wonach ich suchen soll.<br />
Und zwar würde ich gerne wissen, wie ich eine Variable aus test1.cpp in test2.cpp bekomme.<br />
Das sollte also ungefähr so aussehen:</p>
<pre><code class="language-cpp">// Test1.cpp

int hoehe = 5;
</code></pre>
<pre><code class="language-cpp">// Test2.cpp

int breite = 5;
int flaeche = breite * hoehe;
</code></pre>
<p>Wenn ich die Variable hoehe in test1.cpp ganz einfach global initialisiere, kann ich sie aber nicht in test2.cpp nutzen. Nun möchte ich eben gerne wissen wie das geht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1929966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1929966</guid><dc:creator><![CDATA[LustigerAnfänger]]></dc:creator><pubDate>Thu, 22 Jul 2010 11:01:00 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 11:24:22 GMT]]></title><description><![CDATA[<p>Du musst die globalen Variablen in einem Header-File deklarieren. Die Header includest du dann in die *.cpp Files.</p>
<p>Test1.hpp</p>
<pre><code class="language-cpp">#ifndef TEST1_HPP_INCLUDED
#define TEST1_HPP_INCLUDED

extern int hoehe;

#endif
</code></pre>
<p>Test1.cpp</p>
<pre><code class="language-cpp">int hoehe = 5;
</code></pre>
<p>Test2.cpp</p>
<pre><code class="language-cpp">#include &quot;Test1.hpp&quot;

// mach hier was mit hoehe
</code></pre>
<p>Edit: include-guard</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1929980</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1929980</guid><dc:creator><![CDATA[__Stefan__]]></dc:creator><pubDate>Thu, 22 Jul 2010 11:24:22 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 11:25:56 GMT]]></title><description><![CDATA[<p>LustigerAnfänger schrieb:</p>
<blockquote>
<p>Ich habe ein ziemliche Anfängerfrage, habe dazu aber irgendwie nichts gefunden, weil ich nicht weiß wonach ich suchen soll.<br />
Und zwar würde ich gerne wissen, wie ich eine Variable aus test1.cpp in test2.cpp bekomme.<br />
Das sollte also ungefähr so aussehen:</p>
<pre><code class="language-cpp">// Test1.cpp

int hoehe = 5;
</code></pre>
<pre><code class="language-cpp">// Test2.cpp

int breite = 5;
int flaeche = breite * hoehe;
</code></pre>
<p>Wenn ich die Variable hoehe in test1.cpp ganz einfach global initialisiere, kann ich sie aber nicht in test2.cpp nutzen. Nun möchte ich eben gerne wissen wie das geht <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
</blockquote>
<p>Eine Antwort hast du ja bereits. Handelt es sich bei der Frage nur um ein Beispiel oder brauchst du das für dein Projekt tatsächlich so? In dem Fall müsste man überlegen, ob es tatsächlich sinnvoll ist hoehe in der anderen Datei zu deklarieren. Eventuell ließe sich das ganze mit Klassen auch noch besser regeln.</p>
<p>Globale Variablen sind nämlich zu vermeiden, sofern möglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1929983</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1929983</guid><dc:creator><![CDATA[kkkk]]></dc:creator><pubDate>Thu, 22 Jul 2010 11:25:56 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 12:00:56 GMT]]></title><description><![CDATA[<p>Dein Beispiel ist doch eigentlich ein Paradebeispiel für eine klassenlose Funktion.</p>
<pre><code class="language-cpp">int func(int a, int b)
{
return a*b;
}
</code></pre>
<p>lg, freakC++</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1930006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1930006</guid><dc:creator><![CDATA[freakC++]]></dc:creator><pubDate>Thu, 22 Jul 2010 12:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 12:25:50 GMT]]></title><description><![CDATA[<p>freakC++ schrieb:</p>
<blockquote>
<p>Dein Beispiel ist doch eigentlich ein Paradebeispiel für eine klassenlose Funktion.</p>
</blockquote>
<p>Darum ging es doch gar nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1930039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1930039</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 22 Jul 2010 12:25:50 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 12:37:31 GMT]]></title><description><![CDATA[<p>Aber da es sich um eine &quot;Anfängerfrage&quot; handelte, dachte ich dass es ihn vielleicht interessiert. Aber ok ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1930050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1930050</guid><dc:creator><![CDATA[freakC++]]></dc:creator><pubDate>Thu, 22 Jul 2010 12:37:31 GMT</pubDate></item><item><title><![CDATA[Reply to Anfängerfrage on Thu, 22 Jul 2010 12:43:22 GMT]]></title><description><![CDATA[<p>Danke, die Antowort __Stefan__ was genau das, was ich gesucht habe.<br />
Mit einer Funktion habe ich es auch mal probiert, nur fand ich es etwas umständlich wenn man dafür eine Funktion schreiben muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1930056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1930056</guid><dc:creator><![CDATA[LustigerAnfänger]]></dc:creator><pubDate>Thu, 22 Jul 2010 12:43:22 GMT</pubDate></item></channel></rss>