Error: Incompatible function arguments

11.5k Views Asked by At

I'm using pybind11 to access some functions which I wrote in C++ from python. I wish to return an initialized struct instance to python(from a class member function) so that I can access the instance values in python. I have provided a simplified version of my source file. One can reproduce the error with the following. This is the c++ wrapper

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "sum.h"
namespace py = pybind11;

PYBIND11_PLUGIN(GetSum) {

py::module mod("GetSum", "wrapping caltri");

mod.def("get_instance", &GetSum::get_instance, py::return_value_policy::copy, "get instance of out");

py::class_<GetSum, std::shared_ptr<GetSum>>(mod, "GetSum") 
  .def(py::init<>())
  .def("input", &GetSum::input);

return mod.ptr();
}

This is a part of the class header sum.h

   extern "C" {
#include "mesh.h"
}
#include<iostream>
#include<vector>

class GetSum {
 struct A out;

 public:
  GetSum();
  void input(std::vector<float>);
  struct A get_instance() {   
   return out; };  
  ~GetSum();

};

This is class definition sum.cpp

#include "sum.h"

GetSum::GetSum() {
 out.pointlist = nullptr;
}
void GetSum::input(std::vector<float> num){ 
  out.pointlist = new float[num.size()];   
  for(size_t i = 0; i < num.size(); i++) 
      *(out.pointlist+i) = num[i]; 
}

GetSum::~GetSum(){
  delete [] out.pointlist;
}

Struct definition: mesh.h

#include <stdio.h>

struct A {
 float *pointlist;
};

and this is how I was calling it in python.

import GetSum
m = GetSum.GetSum()
m.input([1.0,2.0])
GetSum.s = GetSum.get_instance()

After which I get the error: get_instance(): incompatible function arguments. The following argument types are supported: 1. (arg0: GetSum) -> A

Can someone help me figure out where I might be going wrong?

Thanks!

1

There are 1 best solutions below

0
On

You need to declare struct A to pybind11 before it can be used as a return type by adding at minimum:

py::class_<A>(mod, "A")
  .def(py::init<>());

to your wrapper code. Further, since get_instance is not static, you need to call it as follows in your example python code:

GetSum.s = GetSum.get_instance(m)