<?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[Casten]]></title><description><![CDATA[<p>Ich habe folgenden Code</p>
<pre><code class="language-cpp">SmartPtr&lt;TNLP&gt; mynlp = new KRR_IPOpt( a, K, Ytrain, lambda );
...
status = app-&gt;OptimizeTNLP( mynlp );
...
for( int i = 0; i &lt; a.n_rows; ++i )
	a(i,0) = (*mynlp).a(i,0);
</code></pre>
<p>KRR_IPOpt sieht so aus (Auszug aus dem Header):</p>
<pre><code class="language-cpp">...
class KRR_IPOpt : public TNLP
{
	public:
	mat K;
	vec a;
...
</code></pre>
<p>Wenn ich jetzt kompiliere erhalte ich folgende Fehlermeldung, die ja auch Sinn macht.</p>
<blockquote>
<p>KRR.cpp: In member function ‘void KRR::runOptimization()’:<br />
KRR.cpp:92:21: error: ‘class Ipopt::TNLP’ has no member named ‘a’<br />
make: *** [KRR.o] Error 1</p>
</blockquote>
<p>Wie komme ich durch korrektes casten an die eigentliche Variable ran?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/288762/casten</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 17:25:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/288762.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 22 Jun 2011 12:34:08 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 12:34:08 GMT]]></title><description><![CDATA[<p>Ich habe folgenden Code</p>
<pre><code class="language-cpp">SmartPtr&lt;TNLP&gt; mynlp = new KRR_IPOpt( a, K, Ytrain, lambda );
...
status = app-&gt;OptimizeTNLP( mynlp );
...
for( int i = 0; i &lt; a.n_rows; ++i )
	a(i,0) = (*mynlp).a(i,0);
</code></pre>
<p>KRR_IPOpt sieht so aus (Auszug aus dem Header):</p>
<pre><code class="language-cpp">...
class KRR_IPOpt : public TNLP
{
	public:
	mat K;
	vec a;
...
</code></pre>
<p>Wenn ich jetzt kompiliere erhalte ich folgende Fehlermeldung, die ja auch Sinn macht.</p>
<blockquote>
<p>KRR.cpp: In member function ‘void KRR::runOptimization()’:<br />
KRR.cpp:92:21: error: ‘class Ipopt::TNLP’ has no member named ‘a’<br />
make: *** [KRR.o] Error 1</p>
</blockquote>
<p>Wie komme ich durch korrektes casten an die eigentliche Variable ran?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2082084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082084</guid><dc:creator><![CDATA[HändyÄndy]]></dc:creator><pubDate>Wed, 22 Jun 2011 12:34:08 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 12:38:23 GMT]]></title><description><![CDATA[<p>Es sieht alles etwas kyptisch aus aber wenn ich dich richtig verstanden habe dann so:</p>
<pre><code class="language-cpp">dynamic_cast&lt;KRR_IPOpt*&gt;(mynlp);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2082087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082087</guid><dc:creator><![CDATA[EOutOfResources]]></dc:creator><pubDate>Wed, 22 Jun 2011 12:38:23 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 12:40:40 GMT]]></title><description><![CDATA[<p>EOutOfResources schrieb:</p>
<blockquote>
<p>Es sieht alles etwas kyptisch aus aber wenn ich dich richtig verstanden habe dann so:</p>
<pre><code class="language-cpp">dynamic_cast&lt;KRR_IPOpt*&gt;(mynlp);
</code></pre>
</blockquote>
<p>Vermutlich eher (je nach dem wie es bei dieser Smartpointerklasse heißt:</p>
<pre><code class="language-cpp">dynamic_cast&lt;KRR_IPOpt*&gt;(mynlp.get());
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2082089</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082089</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Wed, 22 Jun 2011 12:40:40 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 12:46:18 GMT]]></title><description><![CDATA[<p>asc schrieb:</p>
<blockquote>
<p>EOutOfResources schrieb:</p>
<blockquote>
<p>Es sieht alles etwas kyptisch aus aber wenn ich dich richtig verstanden habe dann so:</p>
<pre><code class="language-cpp">dynamic_cast&lt;KRR_IPOpt*&gt;(mynlp);
</code></pre>
</blockquote>
<p>Vermutlich eher (je nach dem wie es bei dieser Smartpointerklasse heißt:</p>
<pre><code class="language-cpp">dynamic_cast&lt;KRR_IPOpt*&gt;(mynlp.get());
</code></pre>
</blockquote>
<p>Besser noch: gar nicht erst Typinformationen verlieren (unter der Annahme, dass der Smartpointer etwas taugt und automatische Konvertierungen in SmartPointer auf Basisklassen zulässt).</p>
<pre><code class="language-cpp">SmartPtr&lt;KRR_IPOpt&gt; mynlp = new KRR_IPOpt( a, K, Ytrain, lambda );
...
status = app-&gt;OptimizeTNLP( mynlp );
...
for( int i = 0; i &lt; a.n_rows; ++i )
    a(i,0) = (*mynlp).a(i,0);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2082092</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082092</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 22 Jun 2011 12:46:18 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 13:02:53 GMT]]></title><description><![CDATA[<p>Was camper vorgeschlagen hat, hatte ich auch schon probiert, dann erhalte ich allerdings eine andere Fehlermeldung:</p>
<blockquote>
<p>KRR.cpp:84:36: error: no matching function for call to ‘Ipopt::IpoptApplication::OptimizeTNLP(Ipopt::SmartPtr&lt;KRR_IPOpt&gt;&amp;)’<br />
/homes/numerik/lib/Ipopt/include/coin/IpIpoptApplication.hpp:79:37: note: candidate is: virtual Ipopt::ApplicationReturnStatus Ipopt::IpoptApplication::OptimizeTNLP(const Ipopt::SmartPtr<a href="Ipopt::TNLP" rel="nofollow">Ipopt::TNLP</a>&amp;)<br />
make: *** [KRR.o] Error 1</p>
</blockquote>
<p>Trotzdem vielen Dank für die Hinweise von Euch. Im Header von SmartPtr habe ich nämlich die Methode GetRawPtr() gefunden und meinen Code entsprechend geändert:</p>
<pre><code>a(i,0) = dynamic_cast&lt;KRR_IPOpt*&gt;( GetRawPtr(mynlp) )-&gt;a(i,0);
</code></pre>
<p>Jetzt funktionierts. <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="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2082101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082101</guid><dc:creator><![CDATA[HändyÄndy]]></dc:creator><pubDate>Wed, 22 Jun 2011 13:02:53 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 13:04:12 GMT]]></title><description><![CDATA[<p>Seltsamer Smartpointer... Wer hat den gemacht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2082102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082102</guid><dc:creator><![CDATA[EOutOfResources]]></dc:creator><pubDate>Wed, 22 Jun 2011 13:04:12 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 13:25:41 GMT]]></title><description><![CDATA[<p>Das Problem ist das übliche bei Smartpointern und Ableitungen vom Zieltyp: ein smartpointer&lt;Derived*&gt; lässt sich nicht so einfach automatisch in einen Smartpointer&lt;Base*&gt; umwandeln.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2082119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082119</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 22 Jun 2011 13:25:41 GMT</pubDate></item><item><title><![CDATA[Reply to Casten on Wed, 22 Jun 2011 14:29:40 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Das Problem ist das übliche bei Smartpointern und Ableitungen vom Zieltyp: ein smartpointer&lt;Derived*&gt; lässt sich nicht so einfach automatisch in einen Smartpointer&lt;Base*&gt; umwandeln.</p>
</blockquote>
<p>Ich habe es bisher für üblich gehalten, dass ein smartpointer diese Konvertierung anbietet. Ist ja auch so gut wie immer sehr simpel zu implementieren.<br />
auto_ptr hat diesbzgl. ein Problem, weil es mit den nötigen Tricks kollidiert, die die Movesemantik zu implementieren; aber einen &quot;normalen&quot; Smartpointer trifft diese Problematik nicht.<br />
Im konkreten Falle würde ich es allerdings noch etwas anders machen:</p>
<pre><code class="language-cpp">KRR_IPOpt* mynlp = new KRR_IPOpt( a, K, Ytrain, lambda );
SmartPtr&lt;TNLP&gt; mynlp_smart = mylnp;
...
status = app-&gt;OptimizeTNLP( mynlp_smart );
...
for( int i = 0; i &lt; a.n_rows; ++i )
    a(i,0) = (*mynlp).a(i,0);
</code></pre>
<p>Zumindest sollte es ein static_cast sein. dynamic_casts sind langsam und in diesem Falle logisch fragwürdig. Wir <em>wissen</em> in diesem Falle ja, dass es sich um ein KRR_IPOpt-Objekt handelt.</p>
<p>Ansonsten empfehle ich noch einen Bug-Report an Ipop - habe mir mal kurz die Quellen angeschaut: sieht nicht so aus, als ob Konvertierungen zu irgenwelchen Schwierigkeiten führen würden.<br />
Edit: SmartPtr ist offenbar ein intrusive pointer, also get auch so etwas:</p>
<pre><code class="language-cpp">SmartPtr&lt;KRR_IPOpt&gt; mynlp = new KRR_IPOpt( a, K, Ytrain, lambda );
SmartPtr&lt;TNLP&gt; mynlp_ = GetRawPointer( mynlp );
...
status = app-&gt;OptimizeTNLP( mynlp_ );
...
for( int i = 0; i &lt; a.n_rows; ++i )
    a(i,0) = (*mynlp).a(i,0);
</code></pre>
<p>Wobei ich das für schlechten Stil halte. Der Sinn von intrusive Pointer liegt eigentlich nicht darin, dass man nach Belieben nackte und SMartpointer mischen kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2082144</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2082144</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 22 Jun 2011 14:29:40 GMT</pubDate></item></channel></rss>