<?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[Namespaces nutzen]]></title><description><![CDATA[<p><strong>[gelöst]</strong></p>
<p>Hallo,<br />
ich bin cpp-Neuling und möchte wissen, wie ich namespaces nutze, und dabei gleiche Funktionsnamen benutzen kann. Das habe ich ausprobiert:</p>
<pre><code class="language-cpp">// main.cpp

#include &quot;func1.h&quot;
#include &quot;func2.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

int main(void){
	int z;
	cout &lt;&lt; &quot;Geben Sie eine Zahl ein:&quot;;
	cin &gt;&gt; z;

	{
		using namespace func1;
		ausgabe(z);
	}

	system(&quot;PAUSE&quot;);
	return EXIT_SUCCESS;
}

// func1.h
#ifndef FUNC1_H
#define FUNC1_H

	namespace func1
	{
		const int ANZAHL = 3;
		void ausgabe(int x);
	}

#endif //FUNC1_H 

// func2.h
#ifndef FUNC2_H
#define FUNC2_H

	namespace func2
	{
		const int ANZAHL = 4;
		void ausgabe(int x);
	}

#endif //FUNC2_H 

// func1.cpp
#include &quot;func1.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void ausgabe(int z){
	cout &lt;&lt; &quot;Das ist die Zahl minus 1&quot; &lt;&lt; z-1;
}

// func2.cpp
#include &quot;func2.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void ausgabe(int z){
	cout &lt;&lt; &quot;Das ist die Zahl&quot; &lt;&lt; z;
}

/* Ausgabe beim Kompilieren:

1&gt;  Generating Code...
1&gt;func1.obj : error LNK2005: &quot;void __cdecl ausgabe(int)&quot; (?ausgabe@@YAXH@Z) already defined in func2.obj
1&gt;main.obj : error LNK2019: unresolved external symbol &quot;void __cdecl func1::ausgabe(int)&quot; (?ausgabe@func1@@YAXH@Z) referenced in function _main

*/
</code></pre>
<p>Ich hoffe ihr könnt mir weiterhelfen.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/265957/namespaces-nutzen</link><generator>RSS for Node</generator><lastBuildDate>Thu, 03 Sep 2026 16:41:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/265957.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 01 May 2010 18:11:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Namespaces nutzen on Sat, 01 May 2010 18:33:32 GMT]]></title><description><![CDATA[<p><strong>[gelöst]</strong></p>
<p>Hallo,<br />
ich bin cpp-Neuling und möchte wissen, wie ich namespaces nutze, und dabei gleiche Funktionsnamen benutzen kann. Das habe ich ausprobiert:</p>
<pre><code class="language-cpp">// main.cpp

#include &quot;func1.h&quot;
#include &quot;func2.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

int main(void){
	int z;
	cout &lt;&lt; &quot;Geben Sie eine Zahl ein:&quot;;
	cin &gt;&gt; z;

	{
		using namespace func1;
		ausgabe(z);
	}

	system(&quot;PAUSE&quot;);
	return EXIT_SUCCESS;
}

// func1.h
#ifndef FUNC1_H
#define FUNC1_H

	namespace func1
	{
		const int ANZAHL = 3;
		void ausgabe(int x);
	}

#endif //FUNC1_H 

// func2.h
#ifndef FUNC2_H
#define FUNC2_H

	namespace func2
	{
		const int ANZAHL = 4;
		void ausgabe(int x);
	}

#endif //FUNC2_H 

// func1.cpp
#include &quot;func1.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void ausgabe(int z){
	cout &lt;&lt; &quot;Das ist die Zahl minus 1&quot; &lt;&lt; z-1;
}

// func2.cpp
#include &quot;func2.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void ausgabe(int z){
	cout &lt;&lt; &quot;Das ist die Zahl&quot; &lt;&lt; z;
}

/* Ausgabe beim Kompilieren:

1&gt;  Generating Code...
1&gt;func1.obj : error LNK2005: &quot;void __cdecl ausgabe(int)&quot; (?ausgabe@@YAXH@Z) already defined in func2.obj
1&gt;main.obj : error LNK2019: unresolved external symbol &quot;void __cdecl func1::ausgabe(int)&quot; (?ausgabe@func1@@YAXH@Z) referenced in function _main

*/
</code></pre>
<p>Ich hoffe ihr könnt mir weiterhelfen.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891016</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891016</guid><dc:creator><![CDATA[beuu]]></dc:creator><pubDate>Sat, 01 May 2010 18:33:32 GMT</pubDate></item><item><title><![CDATA[Reply to Namespaces nutzen on Sat, 01 May 2010 18:13:39 GMT]]></title><description><![CDATA[<p>Dass &quot;namespace { .. }&quot; musst du auch in der Source-Datei nutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891017</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891017</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Sat, 01 May 2010 18:13:39 GMT</pubDate></item><item><title><![CDATA[Reply to Namespaces nutzen on Sat, 01 May 2010 18:21:18 GMT]]></title><description><![CDATA[<p>Wie meinst du das?? Ich bin nämlich C++-Neuling und kenne mich nicht so gut aus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1891023</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891023</guid><dc:creator><![CDATA[beuu]]></dc:creator><pubDate>Sat, 01 May 2010 18:21:18 GMT</pubDate></item><item><title><![CDATA[Reply to Namespaces nutzen on Sat, 01 May 2010 18:24:20 GMT]]></title><description><![CDATA[<p>beuu schrieb:</p>
<blockquote>
<pre><code class="language-cpp">// func1.cpp
#include &quot;func1.h&quot;
#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
using namespace std;

void ausgabe(int z){
	cout &lt;&lt; &quot;Das ist die Zahl minus 1&quot; &lt;&lt; z-1;
}
</code></pre>
</blockquote>
<p>Hier definierst du die Funktion &quot;ausgabe&quot; im <strong>globalen</strong> Namespace. Du willst die Funktion jedoch im Namespace func1 definieren. Also wäre folgendes richtig:</p>
<pre><code class="language-cpp">namespace func 1 {
  void ausgabe(int z) { ... }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1891026</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891026</guid><dc:creator><![CDATA[Janjan]]></dc:creator><pubDate>Sat, 01 May 2010 18:24:20 GMT</pubDate></item><item><title><![CDATA[Reply to Namespaces nutzen on Sat, 01 May 2010 18:31:05 GMT]]></title><description><![CDATA[<p>Danke dir <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/1891028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1891028</guid><dc:creator><![CDATA[beuu]]></dc:creator><pubDate>Sat, 01 May 2010 18:31:05 GMT</pubDate></item></channel></rss>