1
votes
I am learning boost-python from the Tutorial, 

but getting an error, can you give me some hint, thanks!

#include <boost/python.hpp>
using namespace boost::python;

struct World
    {
        void set(std::string msg) { this->msg = msg; }
        std::string greet() { return msg; }
        std::string msg;
    };



    BOOST_PYTHON_MODULE(hello)
    {
        class_<World>("World")
            .def("greet", &World::greet)
            .def("set", &World::set)
        ;
    }

Python Terminal:

>>> import hello
>>> planet = hello.World()
>>> planet.set('howdy')

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  Boost.Python.ArgumentError: Python argument types in
  World.set(World, str) did not match C++ signature:
  set(World {lvalue}, std::string)

use the demo code from [boost python Tutorial][1][1]:
https://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

1

1 Answers

1
votes

Change your program to code as below, It will work

#include <boost/python.hpp>
using namespace boost::python;

struct World
    {   
        World(){}; // not mandatory
        void set(std::string msg) { this->msg = msg; }
        std::string greet() { return msg; }
        std::string msg;
    };

    BOOST_PYTHON_MODULE(hello)
    {
        class_<World>("World", init<>())  /* by this line
                 your are giving access to python side 
             to call the constructor of c++ structure World */
                .def("greet", &World::greet)
                .def("set", &World::set)
            ;
    }