[C++-sig] newbie question: converting double* arrays

Andrew Straw strawman at astraw.com
Sat Jul 3 03:26:43 CEST 2004


Continuing the newbie questions:

I'm trying to call a C++ method which takes a double* as an argument.  
(I'm sure this isn't the best C++ style, but it isn't my library I'm 
wrapping!)

After spending the day continuing up the Boost.Python and C++ learning 
curves, I've come up with the very non-ideal solution. It's particularly 
bad because even this hack doesn't compile! So, I seek feedback as to 
either 1) the Right Way to do it or 2) how to fix my presumably simple 
error:

I know the function expects an array of 3 doubles, so I created this hack:

// ---- pyopencv.hpp
class Doubles3
{
public:
  Doubles3(double a, double b, double c);
  double* Get();
protected:
  double values [3];
};

// ---- pyopencv.cpp
#include "pyopencv.hpp"
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <boost/python.hpp>
#include <boost/python/numeric.hpp>
#include <boost/python/tuple.hpp>

Doubles3::Doubles3(double a, double b, double c) { values[0]=a; 
values[1]=b; values[2]=c; }
double* Doubles3::Get() { return values; }

using namespace boost::python;

BOOST_PYTHON_MODULE(pyopencv)
{ 

  class_<Doubles3>("Doubles3", init<double,double,double>())
    .def("Get",&Doubles3::Get)
    //.def("Get",&Doubles3::Get,return_internal_reference<>())
    ;

  class_<std::vector<double> >("vector_of_double")  // Could this be the 
start of the Right Way?
    .def(vector_indexing_suite<std::vector<double> >())
    ;
}


The error in this case is:
/home/astraw/other-peoples-src/boost_1_31_0/boost/python/detail/invoke.hpp:89: 
error: no
   match for call to `(const
   
boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<double*>
   ) (double*)'

And if I try the other .def (commented out above), I get:
/home/astraw/other-peoples-src/boost_1_31_0/boost/python/object/make_instance.hpp:25: 
error: invalid
   application of `sizeof' to an incomplete type

So, I tried putting "double*" between () in 
return_internal_reference<>(), but this didn't help...  I'm in pretty 
deep into C++, as you can tell, but I should think this would be easy, 
even if it's not the best C++ style.  Please help!

For completeness' sake, here's what I'm trying to wrap:

// ---- original method I'm wrapping (in cvaux.h)
class CVAUX_DLL_ENTRY CvCalibFilter
{
public:
    virtual bool
        SetEtalon( CvCalibEtalonType etalonType, double* etalonParams,
                   int pointCount = 0, CvPoint2D32f* points = 0 );
}

// ---- relevant snippet from boosst_cvaux.cpp
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(CvCalibFilter_SetEtalon_overloads, 
CvCalibFilter::SetEtalon, 2, 4)
BOOST_PYTHON_MODULE(cvaux)
{
  class_<CvCalibFilter, CvCalibFilterWrap, 
boost::noncopyable>("CvCalibFilter",
                                   "This is CvCalibFilter's docstring")

    .def("SetEtalon", &CvCalibFilter::SetEtalon,
     CvCalibFilter_SetEtalon_overloads())
}




More information about the Cplusplus-sig mailing list