Boost.Python.ArgumentError:World.set(World,str)中的Python参数类型与C ++签名不匹配:set(World {lvalue},std :: string)

时间:2018-06-01 16:11:31

标签: python c++ boost

return testData.MOCK_DATA.Select(x => new { x.NumberOfCoils, x.DateTime })
            .AsEnumerable()
            .Where(x => x.DateTime> DateTime.Now.AddDays(-numberOfDays))
            .Select(x => new { formattedDate = x.DateTime.ToString("yyyy-MM-dd"), x.NumberOfCoils })
            .OrderBy(x => x.formattedDate)
            .ToList();

但是收到错误,你能给我一些提示吗,谢谢!

I am learning boost-python from the Tutorial, 

Python终端:

#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) ; }
 >>> import hello
 >>> planet = hello.World()

错误:

>>> planet.set('howdy')

使用[boost python中的演示代码   教程] [1] [1]:
 https://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html

1 个答案:

答案 0 :(得分:1)

将您的程序更改为如下代码,它将起作用

#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)
            ;
    }