Docs: Fix several errors of examples from the doc (#592)
* [Doc] Fix several errors of examples from the doc
* Add missing operator def.
* Added missing `()`
* Add missing `namespace`.
diff --git a/docs/advanced/cast/stl.rst b/docs/advanced/cast/stl.rst
index bbd2373..16ac0e6 100644
--- a/docs/advanced/cast/stl.rst
+++ b/docs/advanced/cast/stl.rst
@@ -72,7 +72,7 @@
/* ... binding code ... */
py::class_<MyClass>(m, "MyClass")
- .def(py::init<>)
+ .def(py::init<>())
.def_readwrite("contents", &MyClass::contents);
In this case, properties can be read and written in their entirety. However, an
diff --git a/docs/advanced/classes.rst b/docs/advanced/classes.rst
index e20895e..5843e24 100644
--- a/docs/advanced/classes.rst
+++ b/docs/advanced/classes.rst
@@ -186,7 +186,7 @@
virtual std::string go(int n_times) = 0;
virtual std::string name() { return "unknown"; }
};
- class Dog : public class Animal {
+ class Dog : public Animal {
public:
std::string go(int n_times) override {
std::string result;
@@ -228,7 +228,8 @@
class Husky : public Dog {};
class PyHusky : public Husky {
- using Dog::Dog; // Inherit constructors
+ public:
+ using Husky::Husky; // Inherit constructors
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
@@ -242,11 +243,13 @@
.. code-block:: cpp
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
+ public:
using AnimalBase::AnimalBase; // Inherit constructors
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
};
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
+ public:
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
// Override PyAnimal's pure virtual go() with a non-pure one:
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
@@ -373,7 +376,7 @@
/* ... binding code ... */
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
- .def(py::init<>)
+ .def(py::init<>())
Implicit conversions
====================
@@ -487,6 +490,7 @@
.def(py::self += py::self)
.def(py::self *= float())
.def(float() * py::self)
+ .def(py::self * float())
.def("__repr__", &Vector2::toString);
return m.ptr();
diff --git a/docs/advanced/functions.rst b/docs/advanced/functions.rst
index f291e82..5131144 100644
--- a/docs/advanced/functions.rst
+++ b/docs/advanced/functions.rst
@@ -207,8 +207,8 @@
void print_dict(py::dict dict) {
/* Easily interact with Python types */
for (auto item : dict)
- std::cout << "key=" << item.first << ", "
- << "value=" << item.second << std::endl;
+ std::cout << "key=" << std::string(py::str(item.first)) << ", "
+ << "value=" << std::string(py::str(item.second)) << std::endl;
}
It can be exported:
diff --git a/docs/advanced/pycpp/object.rst b/docs/advanced/pycpp/object.rst
index 8fc165d..8e737cc 100644
--- a/docs/advanced/pycpp/object.rst
+++ b/docs/advanced/pycpp/object.rst
@@ -57,7 +57,7 @@
.. code-block:: cpp
- using pybind11::literals; // to bring in the `_a` literal
+ using namespace pybind11::literals; // to bring in the `_a` literal
f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with