[C++-sig] Boost.Python: Set pointer error.

Christoff Kok christoff.kok at ex-mente.co.za
Mon Oct 5 18:10:11 CEST 2015


Hi,

I am trying to set a raw pointer variable in a wrapped C++ class through
python and I receive the following error:

Boost.Python.ArgumentError: Python argument types in
    None.None(A, P)
did not match C++ signature:
    None(AWrapper {lvalue}, P {lvalue})

This is the C++ code:

#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
#include <vector>

using namespace boost::python;

class P {
private:
    int m_amount = 3;
public:
    int GetAmount() { return m_amount; }
    void SetAmount(int val) { m_amount = val; }
};

class A {
public:
    P* GetP() { return m_p;}
    void SetP(P* val) { m_p = val;}
private:
    P* m_p;
};

class Cont {
public:
    P* createP() {auto p = new P(); m_pList.push_back(p); return p;}
    std::vector<P*>& GetPList() { return m_pList; }
private:
    std::vector<P*> m_pList;
};

struct AWrapper : A, wrapper<A> {
    void wrapper_SetP(P& p) {this->SetP(&p);}
};

BOOST_PYTHON_MODULE(sandbox)
{
    class_<AWrapper, A*>("A", init<>())
        .add_property("p", make_function(&A::GetP,
return_internal_reference<>()), &AWrapper::wrapper_SetP);

    class_<Cont>("Cont", init<>())
        .def("createP", make_function(&Cont::createP,
return_internal_reference<>()))
        .add_property("pList", make_function(&Cont::GetPList,
return_internal_reference<>()));

    class_<P, P*>("P", init<>())
        .add_property("amount", &P::GetAmount, &P::SetAmount);


class_<std::vector<P*>>("PList").def(vector_indexing_suite<std::vector<P*>>());
}

This is the Python code:

import sandbox


cont = sandbox.Cont()

p1 = cont.createP()

print(cont.pList[0].amount)

a = sandbox.A()

a.p = p1

Any help will be greatly appreciated. I've been struggling with this for a
while now.

Regards,
-- 
Christoff Kok
Software Engineer
Ex Mente

http://www.ex-mente.co.za
christoff.kok at ex-mente.co.za
PO Box 10214
Centurion
0046
South Africa
tel: +27 12 743 6993
tel: +27 12 654 8198
fax: +27 85 150 1341
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20151005/04ad2d88/attachment.html>


More information about the Cplusplus-sig mailing list