Skip to content

Commit 9b815ad

Browse files
myd7349wjakob
authored andcommitted
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`.
1 parent 05bc1ff commit 9b815ad

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

docs/advanced/cast/stl.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ functions:
7272
/* ... binding code ... */
7373
7474
py::class_<MyClass>(m, "MyClass")
75-
.def(py::init<>)
75+
.def(py::init<>())
7676
.def_readwrite("contents", &MyClass::contents);
7777
7878
In this case, properties can be read and written in their entirety. However, an

docs/advanced/classes.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ example as follows:
186186
virtual std::string go(int n_times) = 0;
187187
virtual std::string name() { return "unknown"; }
188188
};
189-
class Dog : public class Animal {
189+
class Dog : public Animal {
190190
public:
191191
std::string go(int n_times) override {
192192
std::string result;
@@ -228,7 +228,8 @@ declare or override any virtual methods itself:
228228
229229
class Husky : public Dog {};
230230
class PyHusky : public Husky {
231-
using Dog::Dog; // Inherit constructors
231+
public:
232+
using Husky::Husky; // Inherit constructors
232233
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
233234
std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
234235
std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
@@ -242,11 +243,13 @@ follows:
242243
.. code-block:: cpp
243244
244245
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
246+
public:
245247
using AnimalBase::AnimalBase; // Inherit constructors
246248
std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
247249
std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
248250
};
249251
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
252+
public:
250253
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
251254
// Override PyAnimal's pure virtual go() with a non-pure one:
252255
std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
@@ -373,7 +376,7 @@ crucial that instances are deallocated on the C++ side to avoid memory leaks.
373376
/* ... binding code ... */
374377
375378
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
376-
.def(py::init<>)
379+
.def(py::init<>())
377380
378381
Implicit conversions
379382
====================
@@ -487,6 +490,7 @@ to Python.
487490
.def(py::self += py::self)
488491
.def(py::self *= float())
489492
.def(float() * py::self)
493+
.def(py::self * float())
490494
.def("__repr__", &Vector2::toString);
491495
492496
return m.ptr();

docs/advanced/functions.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ For instance, the following statement iterates over a Python ``dict``:
207207
void print_dict(py::dict dict) {
208208
/* Easily interact with Python types */
209209
for (auto item : dict)
210-
std::cout << "key=" << item.first << ", "
211-
<< "value=" << item.second << std::endl;
210+
std::cout << "key=" << std::string(py::str(item.first)) << ", "
211+
<< "value=" << std::string(py::str(item.second)) << std::endl;
212212
}
213213
214214
It can be exported:

docs/advanced/pycpp/object.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ In C++, the same call can be made using:
5757

5858
.. code-block:: cpp
5959
60-
using pybind11::literals; // to bring in the `_a` literal
60+
using namespace pybind11::literals; // to bring in the `_a` literal
6161
f(1234, "say"_a="hello", "to"_a=some_instance); // keyword call in C++
6262
6363
Unpacking of ``*args`` and ``**kwargs`` is also possible and can be mixed with

docs/classes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ different kinds of input arguments:
298298
struct Pet {
299299
Pet(const std::string &name, int age) : name(name), age(age) { }
300300
301-
void set(int age) { age = age; }
302-
void set(const std::string &name) { name = name; }
301+
void set(int age_) { age = age_; }
302+
void set(const std::string &name_) { name = name_; }
303303
304304
std::string name;
305305
int age;

0 commit comments

Comments
 (0)