Replies: 2 comments 1 reply
-
Hi @hbadi , first of a ctor should be public. Are you sure that you tried it according to the docs? Here you have a working sample: #include <string>
#include <pybind11/pybind11.h>
namespace py = pybind11;
class A
{
public:
A(int n = 1) : _n(n){};
void setVal(int val = 1);
void setVal(std::string, int val = 1);
private:
int _n;
};
PYBIND11_MODULE(foo, module)
{
py::class_<A, std::shared_ptr<A> > cls(module, "A");
cls.def(py::init<int>(), py::arg("n") = 0);
// other bindings...
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @jiwaszki, thanks for your answer Thanks. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Default arguments are not so easy to use in classes with constructor and overloaded methods.
I have the following very basic class
class A {
A(int n=1) : _n(n) {};
void setVal(int val = 1);
void setVal(std::string, int val = 1)
private :
int _n;
};
I can't see how to declare a default argument in an overloaded method and also in the constructor.
I tried using "n"_a=1 and arg("n")=0 but none work, g++ crashes with a lot of errors.
I'd really appreciate any help.
Beta Was this translation helpful? Give feedback.
All reactions