[Ipopt] Using smart pointers

Stefan Vigerske stefan at math.hu-berlin.de
Tue Jul 1 12:30:56 EDT 2008


Hi,

> When I try to compile this, the compiler complains that the new data members are 
> not part of the TNLP class.  For example some of the error messages are below:
> 
> rojects\bmopt\bmopt\createcase.cpp(262) : error C2039: 'm_caseFileName' : is not 
> a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my 
> documents\projects\bmopt\bmopt\createcase.cpp(264) : error C2039: 
> 'm_assetBoundsDB' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my 
> documents\projects\bmopt\bmopt\createcase.cpp(265) : error C2039: 
> 'm_assetBoundsTable' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 1>c:\documents and settings\kris\my 
> documents\projects\bmopt\bmopt\createcase.cpp(267) : error C2039: 
> 'm_assetReturnDB' : is not a member of 'Ipopt::TNLP'
> 1>        c:\optdir\iptnlp.hpp(48) : see declaration of 'Ipopt::TNLP'
> 
> 
> 
> But this is not right, and these members are present in the derived class.  I 
> declare the smart pointer thus:
> 
>     SmartPtr<TNLP> p_optdata;
> and instantiate it as p_optdata = new COpt() where COpt is the derived class 
> from TNLP.

When you try access your object via the p_optdata, then C++ only sees 
this as a TNLP class and does not know anymore that it actually was a 
COpt object.
This is not an issue with smart points but with C++ polymorphism in 
general. Imaging what would happen if you would write TNLP* instead of 
SmartPtr<TNLP>.

> Is there some other way I should treat this derived class?  If any more 
> information is needed, please let me know? 
> 
> I even tried SmarPtr<COpt> p_optdata.  

This should be the (or one) right way.

> The compiler does not like this.  The 
> complaints about the new data members are gone. but instead I get
> 
> 1>c:\documents and settings\kris\my 
> documents\projects\bmopt\bmopt\bmoptdoc.cpp(105) : error C2664: 
> 'Ipopt::IpoptApplication::OptimizeTNLP' : cannot convert parameter 1 from 
> 'Ipopt::SmartPtr<T>' to 'const Ipopt::SmartPtr<T> &'

OK, this is now a SmartPtr issue. The C++ compiler does not know how to 
convert your SmartPtr<COpt> to a SmartPtr<TNLP>, even though COpt is 
derived from TNLP. If it would be COpt* and TNLP*, then the compiler 
would know what to do.
However, you can help him by doing a GetRawPtr(p_optdata). This will 
give you a COpt* point that you can pass into 
IpoptApplication::OptimizeTNLP(...).
Then the SmartPtr<TNLP>::SmarPtr<TNLP>(TNLP*) compiler will be called 
automatically and things should go right.

Or you do it more explicit by saying
SmartPtr<TNLP> p_optdata_tnlp = GetRawPtr(p_optdata);
ipopt->OptimizeTNLP(p_optdata_tnlp).

Best,
Stefan

-- 
Stefan Vigerske
Humboldt University Berlin, Numerical Mathematics
http://www.math.hu-berlin.de/~stefan


More information about the Ipopt mailing list