<?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[function-pointer problem]]></title><description><![CDATA[<p>Hallo, ich verstehe das Problem meines compilers nicht:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

typedef int (*pfcn)(int,int);

int fcn1(int a, int b) { return a + b; }
int fcn2(int a, int b) { return a - b; }
int fcn3(int a, int b) { return a * b; }
int fcn4(int a, int b) { return a / b; }

int main(int argc, char** argv)
{
  std::vector&lt;pfcn&gt; fcn_vec[4];
  fcn_vec.push_back(&amp;fcn1);
  return 0;
}
</code></pre>
<p>compiler</p>
<pre><code>main.cpp: In Funktion »int main(int, char**)«:
main.cpp:14:11: Fehler: Abfrage des Elementes »push_back« in »fcn_vec«, das vom Nicht-Klassentyp »std::vector&lt;int (*)(int, int)&gt; [4]« ist
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/319978/function-pointer-problem</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Jul 2026 15:57:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319978.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 11 Sep 2013 10:39:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:39:51 GMT]]></title><description><![CDATA[<p>Hallo, ich verstehe das Problem meines compilers nicht:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

typedef int (*pfcn)(int,int);

int fcn1(int a, int b) { return a + b; }
int fcn2(int a, int b) { return a - b; }
int fcn3(int a, int b) { return a * b; }
int fcn4(int a, int b) { return a / b; }

int main(int argc, char** argv)
{
  std::vector&lt;pfcn&gt; fcn_vec[4];
  fcn_vec.push_back(&amp;fcn1);
  return 0;
}
</code></pre>
<p>compiler</p>
<pre><code>main.cpp: In Funktion »int main(int, char**)«:
main.cpp:14:11: Fehler: Abfrage des Elementes »push_back« in »fcn_vec«, das vom Nicht-Klassentyp »std::vector&lt;int (*)(int, int)&gt; [4]« ist
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2351800</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351800</guid><dc:creator><![CDATA[afaiko]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:39:51 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:41:45 GMT]]></title><description><![CDATA[<p>Meinten Sie</p>
<pre><code class="language-cpp">std::vector&lt;pfcn&gt; fcn_vec;
</code></pre>
<p>oder</p>
<pre><code class="language-cpp">fcn_vec[0].push_back(&amp;fcn1); // respektive [1], [2] oder [3], je nachdem, welcher der Vektoren im Array gemeint ist.
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351801</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351801</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:41:45 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:41:49 GMT]]></title><description><![CDATA[<p>std::vector&lt;pfcn&gt; fcn_vec[4];</p>
<p>Dadurch legst Du ein Array aus 4 vectoren an. Das ist wohl nicht das was Du wolltest.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351802</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351802</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:41:49 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:47:58 GMT]]></title><description><![CDATA[<p>vielen dank, die [4] habe ich total übersehen danke.</p>
<p>ich stehe erneut auf dem schlauch: warum kann ich nun die ausgabe nicht<br />
bringen:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;

typedef int (*pfcn)(int,int);

int fcn1(int a, int b) { return a + b; }
int fcn2(int a, int b) { return a - b; }
int fcn3(int a, int b) { return a * b; }
int fcn4(int a, int b) { return a / b; }

int main(int argc, char** argv)
{
  std::vector&lt;pfcn&gt; fcn_vec;
  fcn_vec.push_back(&amp;fcn1);
  fcn_vec.push_back(&amp;fcn2);
  fcn_vec.push_back(&amp;fcn3);
  fcn_vec.push_back(&amp;fcn4);

  std::vector&lt;pfcn&gt;::iterator it;
  for(it = fcn_vec.begin(); it != fcn_vec.end(); it++)
     std::cout &lt;&lt; it-&gt;second(2,5) &lt;&lt; std::endl;

  return 0;
}
</code></pre>
<pre><code>main.cpp: In Funktion »int main(int, char**)«:
main.cpp:21:23: Fehler: Abfrage des Elementes »second« in »* it.__gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::operator-&gt; [mit _Iterator = int (**)(int, int), _Container = std::vector&lt;int (*)(int, int)&gt;, __gnu_cxx::__normal_iterator&lt;_Iterator, _Container&gt;::pointer = int (**)(int, int)]()«, das vom Nicht-Klassentyp »int (*)(int, int)« ist
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2351803</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351803</guid><dc:creator><![CDATA[afaiko]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:47:58 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:50:16 GMT]]></title><description><![CDATA[<p>Was soll denn it-&gt;second sein?</p>
<p>So:<br />
std::cout &lt;&lt; (*it)(2,5) &lt;&lt; std::endl;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351804</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351804</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:50:16 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:53:32 GMT]]></title><description><![CDATA[<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> danke Dir</p>
<p>3te und letzte Frage: wie könnte ich obigen code mit einem std::vector realisieren wenn ich jetzt eine zusätzlich fcn5 einführe deren signatur so aussieht:</p>
<p>float fcn5(float a, int b)</p>
<p>? also wenn ich die typen mische?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351805</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351805</guid><dc:creator><![CDATA[afaiko]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:53:32 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 10:57:32 GMT]]></title><description><![CDATA[<p>Pack Funktionszeiger mit anderer Signatur in einen weiteren vector.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351806</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351806</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 11 Sep 2013 10:57:32 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 11:00:46 GMT]]></title><description><![CDATA[<blockquote>
<p>Pack Funktionszeiger mit anderer Signatur in einen weiteren vector.</p>
</blockquote>
<p>ist das die einzige möglichkeit oder ginge es auch anders? Ich frage nur aus Interesse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351808</guid><dc:creator><![CDATA[afaiko]]></dc:creator><pubDate>Wed, 11 Sep 2013 11:00:46 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 11:05:25 GMT]]></title><description><![CDATA[<blockquote>
<p>also wenn ich die typen mische?</p>
</blockquote>
<p>Gar nicht. Du kannst nicht zwei verschiedene Typen in einem Container halten. Macht auch nicht viel Sinn. Hier könntest du den Effekt durch eine Hilfsklasse erzielen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351810</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351810</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Wed, 11 Sep 2013 11:05:25 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 11:06:01 GMT]]></title><description><![CDATA[<p>Nein, ein Funktionszeiger mit einem <code>float</code> ist ein anderer Typ und kann nicht im Vektor abgelegt werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351811</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351811</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Wed, 11 Sep 2013 11:06:01 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 13:32:33 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Du kannst nicht zwei verschiedene Typen in einem Container halten. Macht auch nicht viel Sinn. Hier könntest du den Effekt durch eine Hilfsklasse erzielen.</p>
</blockquote>
<p>Am Ende musst du ja irgendwie wissen, welchen Typ der aus dem Container entnommene Wert hat. Der Container wird's dir nicht sagen, da er nur eine Art von Typ aufnimmt, also sollte der Wert selbst von einer Klasse 'umschlossen' werden, die es dir sagt.<br />
Ein Beispiel ist <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/any.html" rel="nofollow">boost::any</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351848</guid><dc:creator><![CDATA[Youka]]></dc:creator><pubDate>Wed, 11 Sep 2013 13:32:33 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 14:00:56 GMT]]></title><description><![CDATA[<p>Youka schrieb:</p>
<blockquote>
<p>Ein Beispiel ist <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/any.html" rel="nofollow">boost::any</a>.</p>
</blockquote>
<p>Allerdings kann man die meisten Fälle, in denen man <code>boost::any</code> zu benötigen glaubt, sauberer lösen. Zum Beispiel durch dynamische Polymorphie.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351854</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351854</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 11 Sep 2013 14:00:56 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 14:12:35 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Youka schrieb:</p>
<blockquote>
<p>Ein Beispiel ist <a href="http://www.boost.org/doc/libs/1_54_0/doc/html/any.html" rel="nofollow">boost::any</a>.</p>
</blockquote>
<p>Allerdings kann man die meisten Fälle, in denen man <code>boost::any</code> zu benötigen glaubt, sauberer lösen. Zum Beispiel durch dynamische Polymorphie.</p>
</blockquote>
<p>Korrigiere mich bitte, aber läuft das im Endeffekt nicht alles auf Type-Erasure heraus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351863</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351863</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Wed, 11 Sep 2013 14:12:35 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 15:14:49 GMT]]></title><description><![CDATA[<p>Der Unterschied ist allerdings, dass man im Fall von boost::any den genauen dynamischen Typen wissen muss, um mit dem Objekt etwas anfangen zu können.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351903</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351903</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Wed, 11 Sep 2013 15:14:49 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 15:42:40 GMT]]></title><description><![CDATA[<p>krümelkacker schrieb:</p>
<blockquote>
<p>Der Unterschied ist allerdings, dass man im Fall von boost::any den genauen dynamischen Typen wissen muss, um mit dem Objekt etwas anfangen zu können.</p>
</blockquote>
<p>Man muss wissen, welche dynamischen Typen es haben <strong>könnte.</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2351919</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2351919</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Wed, 11 Sep 2013 15:42:40 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 20:15:11 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Nexus schrieb:</p>
<blockquote>
<p>...Zum Beispiel durch dynamische Polymorphie.</p>
</blockquote>
<p>Korrigiere mich bitte, aber läuft das im Endeffekt nicht alles auf Type-Erasure heraus?</p>
</blockquote>
<p>So ist es. Der Wert wird auf dem Heap gelegt und somit der Typ versteckt. Die Zuweisung mit Templates erlaubt jeglichen Eingabetyp, problematisch ist lediglich die Ausgabe. Dazu muss bei Zuweisung auch der Typ gespeichert werden und abgefragt werden können, um den internen Zeiger wieder richtig zu casten.</p>
<p><a href="http://stackoverflow.com/a/4989141" rel="nofollow">Idee hinter boost::any</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2352019</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2352019</guid><dc:creator><![CDATA[Youka]]></dc:creator><pubDate>Wed, 11 Sep 2013 20:15:11 GMT</pubDate></item><item><title><![CDATA[Reply to function-pointer problem on Wed, 11 Sep 2013 21:34:31 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Man muss wissen, welche dynamischen Typen es haben <strong>könnte.</strong></p>
</blockquote>
<p>Und alle durchprobieren, oder wie?</p>
<p>Sone schrieb:</p>
<blockquote>
<p>Korrigiere mich bitte, aber läuft das im Endeffekt nicht alles auf Type-Erasure heraus?</p>
</blockquote>
<p>Nein. Dynamische Polymorphie alleine ist noch keine Type Erasure.</p>
<p>Mit Laufzeitpolymorphie kannst du eine virtuelle Funktion aufrufen, die intern den richtigen dynamischen Typ dispatcht. Bei <code>boost::any</code> muss die Fallunterscheidung explizit erfolgen, was dem Grundgedanken von Polymorphie gerade widerspricht. Deshalb ist <code>boost::any</code> auch nur für Spezialfälle brauchbar, genau wie <code>dynamic_cast</code> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2352055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2352055</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 11 Sep 2013 21:34:31 GMT</pubDate></item></channel></rss>