[C++-sig] Passing by reference from Pythont to C++

Patrick Hartling patrick at vrac.iastate.edu
Fri May 2 18:30:56 CEST 2003


I am pulling my hair out trying to figure out why the following code 
cannot be called from Python:

#include <boost/python.hpp>

void f(float& x)
{
    x = 5.0f;
}

BOOST_PYTHON_MODULE(reftest)
{
    boost::python::def("f", f);
}

When I try to call reftest.f() in Python, I get the dreaded error message 
about a bad argument type:

% python
Python 2.2.1 (#1, Aug 30 2002, 12:15:30)
[GCC 3.2 20020822 (Red Hat Linux Rawhide 3.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import reftest
 >>> x = 0.0
 >>> reftest.f(x)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: bad argument type for built-in operation

Is this an error in how I am using reftest.f() from Python, or is 
something missing from my BPL code?  I've written bindings for a lot of 
other code that used pass-by-reference semantics, but it was always with 
classes rather than floats (or ints or other built-in types).

  -Patrick


-- 
Patrick L. Hartling                     | Research Assistant, VRAC
patrick at vrac.iastate.edu                | 2624 Howe Hall: 1.515.294.4916
http://www.137.org/patrick/             | http://www.vrac.iastate.edu/





More information about the Cplusplus-sig mailing list