<?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[Stringarray inizialiesieren]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich habe ein Problem und zwar will das hier nicht funktionieren:</p>
<pre><code class="language-cpp">std::string keywords[] = { &quot;Eins&quot;   , &quot;eins&quot;   , &quot;EINS&quot;   ,
                           &quot;Zwei&quot;   , &quot;zwei&quot;   , &quot;ZWEI&quot;   ,
                           &quot;Drei&quot;   , &quot;drei&quot;   , &quot;DREI&quot;   ,
                           &quot;Vier&quot;   , &quot;vier&quot;   , &quot;VIER&quot;   ,
                           &quot;Fünf&quot;   , &quot;fünf&quot;   , &quot;FÜNF&quot;   ,
                           &quot;Sechs&quot;  , &quot;sechs&quot;  , &quot;SECHS&quot;  ,
                           &quot;Sieben&quot; , &quot;sieben&quot; , &quot;SIEBEN&quot; ,
                           &quot;Acht&quot;   , &quot;acht&quot;   , &quot;ACHT&quot;   ,
                           &quot;Neun&quot;   , &quot;neun&quot;   , &quot;NEUN&quot;   ,
                           &quot;Zehn&quot;   , &quot;zehn&quot;   , &quot;ZEHN&quot;   };
</code></pre>
<p>Warum nicht und wie kann ich das lösen?^^</p>
<p>Mfg Hans <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>
]]></description><link>https://www.c-plusplus.net/forum/topic/289631/stringarray-inizialiesieren</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 00:58:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/289631.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 09 Jul 2011 19:30:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 19:30:14 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich habe ein Problem und zwar will das hier nicht funktionieren:</p>
<pre><code class="language-cpp">std::string keywords[] = { &quot;Eins&quot;   , &quot;eins&quot;   , &quot;EINS&quot;   ,
                           &quot;Zwei&quot;   , &quot;zwei&quot;   , &quot;ZWEI&quot;   ,
                           &quot;Drei&quot;   , &quot;drei&quot;   , &quot;DREI&quot;   ,
                           &quot;Vier&quot;   , &quot;vier&quot;   , &quot;VIER&quot;   ,
                           &quot;Fünf&quot;   , &quot;fünf&quot;   , &quot;FÜNF&quot;   ,
                           &quot;Sechs&quot;  , &quot;sechs&quot;  , &quot;SECHS&quot;  ,
                           &quot;Sieben&quot; , &quot;sieben&quot; , &quot;SIEBEN&quot; ,
                           &quot;Acht&quot;   , &quot;acht&quot;   , &quot;ACHT&quot;   ,
                           &quot;Neun&quot;   , &quot;neun&quot;   , &quot;NEUN&quot;   ,
                           &quot;Zehn&quot;   , &quot;zehn&quot;   , &quot;ZEHN&quot;   };
</code></pre>
<p>Warum nicht und wie kann ich das lösen?^^</p>
<p>Mfg Hans <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2090739</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090739</guid><dc:creator><![CDATA[Hans321]]></dc:creator><pubDate>Sat, 09 Jul 2011 19:30:14 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 19:36:44 GMT]]></title><description><![CDATA[<p>Jetzt wäre es natürlich noch nützlich zu wissen, wie sich dieses &quot;funktioniert nicht&quot; überhaupt bemerkbar macht. In dem Code sehe ich zumindest keinen Fehler.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090743</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090743</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Sat, 09 Jul 2011 19:36:44 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 19:44:07 GMT]]></title><description><![CDATA[<p>Wenn ich das versuche wie folgt in der main zu definieren dann sagt der gcc:</p>
<blockquote>
<p>/home/michael/Dokumente/Development Sector/Forge/Playground/Number/Number.cpp|47|error: expected primary-expression before ‘]’ token|</p>
<p>/home/michael/Dokumente/Development Sector/Forge/Playground/Number/Number.cpp|56|warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x|</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2090749</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090749</guid><dc:creator><![CDATA[Hans321]]></dc:creator><pubDate>Sat, 09 Jul 2011 19:44:07 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 19:55:35 GMT]]></title><description><![CDATA[<p>Hmm, also bei mir funktionierte das Array <a href="http://ideone.com/dCgl7" rel="nofollow">problemlos</a>. Das einzige Problem wäre, daß du eventuell ein <code>#include &lt;string&gt;</code> vergessen haben könntest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090755</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090755</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Sat, 09 Jul 2011 19:55:35 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 20:04:22 GMT]]></title><description><![CDATA[<p>Okay vllt liegt der fehler auch doch wo anders.<br />
Also ich habe in einer klasse &quot;std::string keywords[];&quot; als private, dass soll dan pasieren:</p>
<pre><code class="language-cpp">Number::Number(string w)
{
    this-&gt;written = w;
    Init();
    ReadString(this-&gt;written);
}
</code></pre>
<pre><code class="language-cpp">void Number::Init()
{
    this-&gt;keywords[] = { &quot;Eins&quot;   , &quot;eins&quot;   , &quot;EINS&quot;   ,
                         &quot;Zwei&quot;   , &quot;zwei&quot;   , &quot;ZWEI&quot;   ,
                         &quot;Drei&quot;   , &quot;drei&quot;   , &quot;DREI&quot;   ,
                         &quot;Vier&quot;   , &quot;vier&quot;   , &quot;VIER&quot;   ,
                         &quot;Fünf&quot;   , &quot;fünf&quot;   , &quot;FÜNF&quot;   ,
                         &quot;Sechs&quot;  , &quot;sechs&quot;  , &quot;SECHS&quot;  ,
                         &quot;Sieben&quot; , &quot;sieben&quot; , &quot;SIEBEN&quot; ,
                         &quot;Acht&quot;   , &quot;acht&quot;   , &quot;ACHT&quot;   ,
                         &quot;Neun&quot;   , &quot;neun&quot;   , &quot;NEUN&quot;   ,
                         &quot;Zehn&quot;   , &quot;zehn&quot;   , &quot;ZEHN&quot;   };
}
</code></pre>
<p>Mfg hans</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090761</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090761</guid><dc:creator><![CDATA[hans321]]></dc:creator><pubDate>Sat, 09 Jul 2011 20:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 20:22:24 GMT]]></title><description><![CDATA[<p>Was ist keywords? Und was sollen die eckigen Klammern dahinter? Das ist doch keine Deklaration! Du machst es einem nicht leicht, Dir zu helfen. Wenn Du mehr verrätst (sowohl Code-technisch als auch was du überhaupt machen willst), kann man Sie auch besser helfen. <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>
<p>Tipp: Soll es eine konstante String-Tabelle werden, ist vielleicht</p>
<pre><code class="language-cpp">const char* const table[] = {&quot;dings&quot;, &quot;bums&quot;};
</code></pre>
<p>keine schlechte Idee, da Du Dir damit die dynamische Initialisierung der std::string-Objekte sparst. Als Faustregel würde ich sagen &quot;Ziehe die Verwendung von std::string gegenüber char* vor -- es sei denn, es handelt sich um String-KONSTANTEN&quot;.</p>
<p>Einfach mehr verraten. Dir fehlt anscheinend einiges an Grundwissen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090769</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090769</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sat, 09 Jul 2011 20:22:24 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 20:16:58 GMT]]></title><description><![CDATA[<p>Nein, an der Stelle kannst du es nicht so verwenden - eine Initialisierungsliste kannst du nur direkt bei der Definition einer Variable verwenden, nicht bei einer nachträglichen Zuweisung. (und einem kompletten Array kannst du auch nichts zuweisen)</p>
<p>Für deine Konstruktion wäre es eventuell eine gute Idee, dieses Array als statisches Element der Klasse zu definieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090770</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090770</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Sat, 09 Jul 2011 20:16:58 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 23:53:18 GMT]]></title><description><![CDATA[<blockquote>
<p>Als Faustregel würde ich sagen &quot;Ziehe die Verwendung von std::string gegenüber char* vor -- es sei denn, es handelt sich um String-KONSTANTEN&quot;.</p>
</blockquote>
<p>Warum const char* bei string konstanten? Da würde ich auch ein const std::string für sinnvoller halten <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>
<blockquote>
<p>Nein, an der Stelle kannst du es nicht so verwenden - eine Initialisierungsliste kannst du nur direkt bei der Definition einer Variable verwenden, nicht bei einer nachträglichen Zuweisung. (und einem kompletten Array kannst du auch nichts zuweisen)</p>
<p>Für deine Konstruktion wäre es eventuell eine gute Idee, dieses Array als statisches Element der Klasse zu definieren.</p>
</blockquote>
<p>Ah <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /><br />
Das ist mir wohl irgendwo entgangen <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="😃"
    /><br />
Habs aber mal ausprobiert und es geht immer noch nicht.</p>
<pre><code class="language-cpp">#ifndef NUMBER_HPP
#define NUMBER_HPP

#include &lt;string&gt;

namespace Playground
{
    class Number
    {
        private:
            int normal; // The Digit
            std::string written; // The String
            static const std::string keywords[] = { &quot;Eins&quot;   , &quot;eins&quot;   , &quot;EINS&quot;   ,
                             &quot;Zwei&quot;   , &quot;zwei&quot;   , &quot;ZWEI&quot;   ,
                             &quot;Drei&quot;   , &quot;drei&quot;   , &quot;DREI&quot;   ,
                             &quot;Vier&quot;   , &quot;vier&quot;   , &quot;VIER&quot;   ,
                             &quot;Fünf&quot;   , &quot;fünf&quot;   , &quot;FÜNF&quot;   ,
                             &quot;Sechs&quot;  , &quot;sechs&quot;  , &quot;SECHS&quot;  ,
                             &quot;Sieben&quot; , &quot;sieben&quot; , &quot;SIEBEN&quot; ,
                             &quot;Acht&quot;   , &quot;acht&quot;   , &quot;ACHT&quot;   ,
                             &quot;Neun&quot;   , &quot;neun&quot;   , &quot;NEUN&quot;   ,
                             &quot;Zehn&quot;   , &quot;zehn&quot;   , &quot;ZEHN&quot;   }; // Digits as Words
        public:
            Number(int n);
            Number(std::string w);

            int GetDigit() const;
            std::string GetString() const;

            void WriteString(int i);
            void ReadString(const std::string s);
        private:
            void Init(); // Inits keywords with Strings!
    };
}

#endif // NUMBER_HPP
</code></pre>
<p>Fehlermeldung:</p>
<blockquote>
<p>/home/michael/Dokumente/Development Sector/Forge/Playground/Number/Number.hpp|13|error: a brace-enclosed initializer is not allowed here before ‘{’ token|<br />
/home/michael/Dokumente/Development Sector/Forge/Playground/Number/Number.hpp|22|error: invalid in-class initialization of static data member of non-integral type ‘const std::string []’|<br />
||=== Build finished: 2 errors, 0 warnings ===|</p>
</blockquote>
<p>Warum das jetzt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090848</guid><dc:creator><![CDATA[hans321]]></dc:creator><pubDate>Sat, 09 Jul 2011 23:53:18 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sat, 09 Jul 2011 23:56:04 GMT]]></title><description><![CDATA[<p>Ahja wer wissen will was das werden soll.<br />
Eine Zahl die sich auch ausgeschrieben abrufen lässt.<br />
Ich hab schon längere zeit nicht mehr programmiert und bin ein wenig eingerostet <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2090850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090850</guid><dc:creator><![CDATA[Hans321]]></dc:creator><pubDate>Sat, 09 Jul 2011 23:56:04 GMT</pubDate></item><item><title><![CDATA[Reply to Stringarray inizialiesieren on Sun, 10 Jul 2011 01:34:41 GMT]]></title><description><![CDATA[<p>Vom Design her ist es schon mal fraglich. Ich sehe den Sinn der eigenen Number-Klasse nämlich nicht. Für die Konvertierung Zeichenkette-&gt;Int tut es auch eine freie Funktion. Und diese freie Funktion könnte eine map&lt;string,int&gt; benutzen.</p>
<p>Dein Code funktioniert nicht, weil statische Datenelemente innerhalb der Klasse nur deklariert werden können. Die Definition findet außerhalb der Klasse statt. Und bei der Definition darfst Du dann auch eine Initialisierung mit angeben. C++ lässt Dich lediglich einen Initialisierer für Ganzzahlkonstanten angeben. Die gibt es hier aber nicht.</p>
<p>Bzgl String-Konstanten: Schreibst Du</p>
<pre><code class="language-cpp">const char* const p = &quot;huhu&quot;;
</code></pre>
<p>packt der Compiler in das Kompilat die Zeichenkette rein. Die Zeichenkette wird im statischen Speicherbereich liegen (man muss sich also nicht um die Speicherverwaltung kümmern) und der Zeiger zeigt einfach auf das erste Zeichen der Teichenkette. Schreibst Du dagegen</p>
<pre><code class="language-cpp">const std::string q = &quot;huhu&quot;;
</code></pre>
<p>auf globaler Ebene wird auch die Zeichenkette in das Kompilat mit eingefügt. Dazu kommt dynamischer Initialisierungscode. Bevor die Main-Funktion aufgerufen wird, muss dieses String-Objekt nämlich initialisiert werden. Der Konstruktor läuft also vor der main-Funktion (daher &quot;dynamische Initialisierung&quot;) und Dieser Konstruktor reserviert nochmal extra Speicher aus dem Freispeicher und kopiert Zeichen für Zeichen von der String-Konstante aus dem statischen Speicher zum Freispeicher. Dieser Aufwand ist komplett überflüssig, wenn Du q zwischendurch nicht ändern möchtest.</p>
<p>Ein Array von Zeichenketten ist für Deine Anwendung auch nur bedingt sinnvoll. Du musst es hier linear durchlaufen und jedes Element mit der Eingabe vergleichen. Das ist für 30 Elemente noch okay, wird aber schnell zu aufwändig. Dafür gibt es nämlich so Dinge wie map&lt;string,int&gt;.</p>
<p>Also, Pseudocode...</p>
<pre><code class="language-cpp">string allesgross(string);
string allesklein(string);

map&lt;string,int&gt; erzeuge_sortierte_tabelle()
{
  static const char* const konstanten[] = {
    &quot;Null&quot;, &quot;Eins&quot;, &quot;Zwei&quot;, &quot;Drei&quot;, &quot;Vier&quot;
  };
  const int kanzahl = sizeof(konstanten)/sizeof(konstanten[0]);
  map&lt;string,int&gt; tab;
  for (int i=0; i&lt;kanzahl; ++i) {
    tab[           konstanten[i] ] = i;
    tab[allesgross(konstanten[i])] = i;
    tab[allesklein(konstanten[i])] = i;
  }
  return tab;
}

map&lt;string,int&gt; const&amp; meine_tabelle() {
  static map&lt;string,int&gt; tab = erzeuge_sortierte_tabelle();
  return tab;
}

int konvertiere_wort_zu_zahl(string const&amp; s) {
  map&lt;string,int&gt; const&amp; t = meine_tabelle();
  auto iterator = t.find(s);
  if (iterator==t.end()) throw ...einen fehler melden...;
  return iterator-&gt;second;
}
</code></pre>
<p>Man muss nicht alles als Klasse verpacken! Du darfst auch freie Funktionen schreiben und verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2090859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2090859</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Sun, 10 Jul 2011 01:34:41 GMT</pubDate></item></channel></rss>