pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: How to bind a c array within a class function

Open Stoneplay opened this issue 1 year ago • 0 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

What version (or hash if on master) of pybind11 are you using?

2.11.1

Problem description

Due to the set_max_vel function of Engine class is not static function, so it will raise an error when I bind the the set_max_vel function like that. How to bind a c array which is in a function within a class?

Reproducible example code

// c++ code
#include <Eigen/Core>

class Engine: {
public:
    Engine();
    void set_max_vel(float *max_vel, int dof){
        max_velocity_.resize(dof);
	for (int i = 0; i < dof; i++){
            max_velocity_[i] = (double)max_vel[i];
	}
    }
private:
    Eigen::VectorXd max_velocity_;
}

// binding code
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

py::class_<Engine>(m, "Engine")
    .def(py::init<>())
    .def("set_max_vel", [](py::array_t<float> buffer, int dof) {
        py::buffer_info info = buffer.request();
	Engine::set_max_vel(static_cast<float*>(info.ptr), dof); });

Is this a regression? Put the last known working version here if it is.

Not a regression

Stoneplay avatar Mar 01 '24 11:03 Stoneplay