0

I'm trying to wrap a simple demo class with a function called "addTwoNumbers" with boost python. Here's the header file:

#ifndef DEMO_H_
#define DEMO_H_

#include <boost/function.hpp>

class Demo
{
public:
    Demo() {}
    virtual ~Demo() {}

    typedef void (DemoCb) (int,int,int);
    boost::function<DemoCb> onAddTwoNumbers;

    int addTwoNumbers(int x, int y);

    // Executes a callback within a thread not controlled by the caller.
    void addTwoNumbersAsync(int x, int y, boost::function<DemoCb> callback);

};

#endif /* DEMO_H_ */

And here's the wrapping:

#include <boost/python.hpp>
#include "../demo.h"
using namespace boost::python;

// Create a python module using boost. The name 'demo' must match the name in the makefile
BOOST_PYTHON_MODULE(python_wrap_demo) {
    // Wrapping the addTwoNumbers function:
    class_<Demo>("Demo", init<>())
        .def("addTwoNumbers", Demo::addTwoNumbers)
    ;
}

I got this to work for a similar function, which wasn't wrapped in a class. Why am I now getting this error?

mavix
  • 2,488
  • 6
  • 30
  • 52

2 Answers2

1

I'm not familiar with boost::python, but I believe you just need & to pass the address of the member .def("addTwoNumbers", &Demo::addTwoNumbers). Non-member functions and static member functions can be implicitly converted to function pointers, but non-static member functions are different and you need & to pass the address.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
0

The error message is very clear; addTwoNumbers is a member function of Demo, not a static function, yet you are attempting to call it as a static function. You must have an instance of Demo to call it upon.

In your case, addTwoNumbers does not need to be a member function, so simply make it static. For future reference see: http://www.parashift.com/c++-faq/pointers-to-members.html

Ed S.
  • 122,712
  • 22
  • 185
  • 265