<?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[statische felder -- mal wieder]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe eine Klasse A, welche ein statisches Feld definiert, in dieser Art (A.h):</p>
<pre><code>class A {
        public:

            typedef unsigned int ResourceValue;

            static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;
            A(const ResourceValue &amp; rv);
</code></pre>
<p>Nutzen möchte ich dies in einer Klasse B wie folgt:</p>
<pre><code>A a(A::MINIMUM_RESOURCE_VALUE);
</code></pre>
<p>Compiliert wird, allerdings sagt mir der Linker:</p>
<pre><code>undefined reference to `A::MINIMUM_RESOURCE_VALUE'
</code></pre>
<p>Was mach ich den schon wieder falsch?????</p>
<p>Schon mal dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323713/statische-felder-mal-wieder</link><generator>RSS for Node</generator><lastBuildDate>Wed, 15 Jul 2026 17:04:05 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323713.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 12 Feb 2014 09:54:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 09:54:24 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe eine Klasse A, welche ein statisches Feld definiert, in dieser Art (A.h):</p>
<pre><code>class A {
        public:

            typedef unsigned int ResourceValue;

            static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;
            A(const ResourceValue &amp; rv);
</code></pre>
<p>Nutzen möchte ich dies in einer Klasse B wie folgt:</p>
<pre><code>A a(A::MINIMUM_RESOURCE_VALUE);
</code></pre>
<p>Compiliert wird, allerdings sagt mir der Linker:</p>
<pre><code>undefined reference to `A::MINIMUM_RESOURCE_VALUE'
</code></pre>
<p>Was mach ich den schon wieder falsch?????</p>
<p>Schon mal dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382772</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382772</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 09:54:24 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 09:58:39 GMT]]></title><description><![CDATA[<p>Du schaffst es nicht halbwegs ordentlichen Code hier zu posten, kann das sein?!<br />
Der Code hier ist so nicht im Ansatz kompilierbar (mit dem was ich mir grad dazu gespinnt habe, ist er das schon und auch noch fehlerfrei) und in deinem Thread mit den verschiedenen Timerklassen in einer Datenstruktur war der Beispielcode noch weit weit schlimmer.</p>
<p>Naja, egal.</p>
<pre><code>class A {
public:
	typedef unsigned int ResourceValue;

	static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;
	A(const ResourceValue &amp; rv){}
};

int main()
{
	A a(A::MINIMUM_RESOURCE_VALUE);

	return 0x0;
}
</code></pre>
<p>Ideone (und damit GCC) meckert nicht.</p>
<p>Ist das der richtige Code?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382774</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382774</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 12 Feb 2014 09:58:39 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:03:02 GMT]]></title><description><![CDATA[<p>Minimales, kompilierbares Beispiel ? Das was du bisher geschrieben hast lässt auf keinen Fehler schließen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382775</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382775</guid><dc:creator><![CDATA[DarkShadow44]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:03:02 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:12:32 GMT]]></title><description><![CDATA[<p>ok ok. Sorry jungs. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Also wenn ich dies hier baue:</p>
<pre><code>/*
 * File:   main.cc
 * Created on February 12, 2014, 11:05 AM
 */

#include &lt;cstdlib&gt;

using namespace std;

class A {
public:

    typedef unsigned int ResourceValue;
    static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;
    static const ResourceValue MAXIMUM_RESOURCE_VALUE = 10000;

    A() {
    }

    A(const ResourceValue &amp; rv) {
    }
};

/*
 *
 */
int
main(int argc, char** argv) {

    A newResources(A::MINIMUM_RESOURCE_VALUE);
    return 0;
}
</code></pre>
<p>Bekomme ich:</p>
<pre><code>mkdir -p build/Debug/GNU-Linux-x86
rm -f &quot;build/Debug/GNU-Linux-x86/main.o.d&quot;
g++    -c -g -MMD -MP -MF &quot;build/Debug/GNU-Linux-x86/main.o.d&quot; -o build/Debug/GNU-Linux-x86/main.o main.cc
mkdir -p dist/Debug/GNU-Linux-x86
g++     -o dist/Debug/GNU-Linux-x86/test build/Debug/GNU-Linux-x86/main.o 
build/Debug/GNU-Linux-x86/main.o: In function `main':
main.cc:43: undefined reference to `A::MINIMUM_RESOURCE_VALUE'
collect2: ld returned 1 exit status
gmake[2]: *** [dist/Debug/GNU-Linux-x86/test] Error 1
</code></pre>
<p>Das ganze auf CentOs mit gcc(GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)</p>
<p>Besser???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382778</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382778</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:12:32 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:29:25 GMT]]></title><description><![CDATA[<p>so gehts übrigends:</p>
<pre><code>int
main(int argc, char** argv) {

    A::ResourceValue a = A::MINIMUM_RESOURCE_VALUE;
    A newResources(a);
    return 0;
}
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382785</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382785</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:29:25 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:32:45 GMT]]></title><description><![CDATA[<p>und auch so:</p>
<p>[code=&quot;cpp&quot;]<br />
class A {<br />
public:<br />
typedef unsigned int ResourceValue;</p>
<p>static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;<br />
A(const ResourceValue rv){} //&lt;&lt;&lt;----<br />
};</p>
<p>int main()<br />
{<br />
A a(A::MINIMUM_RESOURCE_VALUE);</p>
<p>return 0x0;<br />
}<br />
[/cpp]</p>
<p>Liegt es am typedef???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382787</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382787</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:32:45 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:33:35 GMT]]></title><description><![CDATA[<p><a href="http://ideone.com/kctUFA" rel="nofollow">Nix Fehler</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382788</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382788</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:33:35 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:37:46 GMT]]></title><description><![CDATA[<p>Du musst die statischen Member definieren.</p>
<pre><code>class A {
public:

    typedef unsigned int ResourceValue;
    static const ResourceValue MINIMUM_RESOURCE_VALUE = 0;
    static const ResourceValue MAXIMUM_RESOURCE_VALUE = 10000;

    A() {
    }

    A(const ResourceValue &amp; rv) {
    }
};

const A::ResourceValue A::MINIMUM_RESOURCE_VALUE;
const A::ResourceValue A::MAXIMUM_RESOURCE_VALUE;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2382789</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382789</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:37:46 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:38:45 GMT]]></title><description><![CDATA[<p>Furble Wurble schrieb:</p>
<blockquote>
<p>Du musst die statischen Member definieren.</p>
</blockquote>
<p>Bei integralen Datentypen nicht, soweit ich weiß.</p>
<p>Sieht aus, als wäre der Compiler kaputt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382790</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382790</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:38:45 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:43:41 GMT]]></title><description><![CDATA[<p>MFK schrieb:</p>
<blockquote>
<p>Sieht aus, als wäre der Compiler kaputt.</p>
</blockquote>
<p>Ich vermute der hat in dem Fall sogar recht.<br />
Wenn man die Referenz im Konstruktor entfernt, dann gehts.....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382791</guid><dc:creator><![CDATA[somebody123]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:54:09 GMT]]></title><description><![CDATA[<p>MFK schrieb:</p>
<blockquote>
<p>Furble Wurble schrieb:</p>
<blockquote>
<p>Du musst die statischen Member definieren.</p>
</blockquote>
<p>Bei integralen Datentypen nicht, soweit ich weiß.</p>
<p>Sieht aus, als wäre der Compiler kaputt.</p>
</blockquote>
<p>Nein, das ist ein &quot;Feature&quot; (andere würden es &quot;Fehlerverschleierung&quot; nennen) bestimmter Compiler. Andere Implementierungen melden hier streng nach ISO-Standard einen Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382792</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382792</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:54:09 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:55:30 GMT]]></title><description><![CDATA[<p>das ist ja in der art komisch.</p>
<p>wie gesagt, ohne reference im konstruktor geht es. auch wie es Furble Wurble vorgeschlagen hat. was ist nun korrekt????</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/18806">@Skym0sh0</a> der link geht bei mir nicht. lädt ewig ohne das was kommt. was hätte man da gesehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382793</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382793</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:55:30 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:56:58 GMT]]></title><description><![CDATA[<p>ah, da war SeppJ schneller. also ist die variante von Furble Wurble die standard konforme?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382794</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382794</guid><dc:creator><![CDATA[sven_]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:56:58 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 10:57:39 GMT]]></title><description><![CDATA[<p>MFK schrieb:</p>
<blockquote>
<p>Furble Wurble schrieb:</p>
<blockquote>
<p>Du musst die statischen Member definieren.</p>
</blockquote>
<p>Bei integralen Datentypen nicht, soweit ich weiß.</p>
<p>Sieht aus, als wäre der Compiler kaputt.</p>
</blockquote>
<p>Ich versuch mich mal wieder am Standard <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><strong>9.4.2 Static data members</strong><br />
2<br />
<strong>The declaration of a static data member in its class definition is not a definition</strong> and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition.[...]</p>
<p>3<br />
If <strong>a non-volatile const static data member is of integral or enumeration type</strong>, its declaration in the class definition can specify a brace-or-equal-initializer [...] <strong>The member shall still be defined in a namespace scope if it is odr-used (3.2)</strong> in the program and the namespace scope definition shall not contain an initializer.</p>
</blockquote>
<p>somebody123 schrieb:</p>
<blockquote>
<p>MFK schrieb:</p>
<blockquote>
<p>Sieht aus, als wäre der Compiler kaputt.</p>
</blockquote>
<p>Ich vermute der hat in dem Fall sogar recht.<br />
Wenn man die Referenz im Konstruktor entfernt, dann gehts.....</p>
</blockquote>
<p>[Spekulation]Wahrscheinlich kriegt der Linker hier irgendwas zu fassen, wenn der Typ in der Signatur des C'tors auftaucht.[/Spekulation]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382795</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382795</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Wed, 12 Feb 2014 10:57:39 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Wed, 12 Feb 2014 11:01:02 GMT]]></title><description><![CDATA[<p>Skym0sh0 schrieb:</p>
<blockquote>
<p><a href="http://ideone.com/kctUFA" rel="nofollow">Nix Fehler</a></p>
</blockquote>
<p>Ah: das <a href="http://ideone.com" rel="nofollow">ideone.com</a> jetzt down ist, beendet meinen Beweis.<br />
q.e.d.</p>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2382797</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2382797</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Wed, 12 Feb 2014 11:01:02 GMT</pubDate></item><item><title><![CDATA[Reply to statische felder -- mal wieder on Fri, 14 Feb 2014 12:51:53 GMT]]></title><description><![CDATA[<p>@sven_<br />
Die Variante mit Referenz und mit Definition ist OK.<br />
Die Variante ohne Referenz und ohne Definition ist auch OK.<br />
Nicht OK ist einzig die Variante mit Referenz aber ohne Definition. Denn dort wird das &quot;Objekt&quot; MINIMUM_RESOURCE_VALUE verwendet (Lvalue), nicht bloss der Wert (Rvalue). Und sobald das Objekt selbst verwendet wird, muss es auch definiert werden.</p>
<p>Wobei viele Compiler sich trotzdem nicht an sowas stören.</p>
<p>Ich finde diese Regel übrigens total doof. Technisch müssen Compiler bereits um Templates zu ermöglichen alles drauf haben, was nötig wäre, um die implizite Definition solcher static const zu ermöglichen.<br />
Also sollten sie es verdammtnochmal auch tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2383174</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2383174</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Fri, 14 Feb 2014 12:51:53 GMT</pubDate></item></channel></rss>