TypeError:没有C++的TyPython(按值)转换器 我试图使用Posith.python将我的C++类暴露给Python。下面是我尝试做的一个简单版本: struct Base { virtual ~Base() {}; virtual char const *Hello() { printf("Base.Hello\n"); return "Hello. I'm Base."; }; }; struct Derived : Base { char const *Hello() { printf("Derived.Hello\n"); return "Hello. I'm Derived."; }; Base &test() { printf("Derived.test\n"); // ... // After some calculation, we get result reference `instance' // `instance' can be an instance of Base or Derived. // ... return instance; } };

TypeError:没有C++的TyPython(按值)转换器 我试图使用Posith.python将我的C++类暴露给Python。下面是我尝试做的一个简单版本: struct Base { virtual ~Base() {}; virtual char const *Hello() { printf("Base.Hello\n"); return "Hello. I'm Base."; }; }; struct Derived : Base { char const *Hello() { printf("Derived.Hello\n"); return "Hello. I'm Derived."; }; Base &test() { printf("Derived.test\n"); // ... // After some calculation, we get result reference `instance' // `instance' can be an instance of Base or Derived. // ... return instance; } };,python,c++,boost,polymorphism,boost-python,Python,C++,Boost,Polymorphism,Boost Python,我希望在python中使用上述类,如下所示: instance = Derived() // If method test returns an instance of Base instance.test().Hello() // Result: "Hello. I'm Base." // If method test returns an instance of Derived instance.test().Hello() // Result: "Hello. I'm Derived.

我希望在python中使用上述类,如下所示:

instance = Derived()

// If method test returns an instance of Base
instance.test().Hello() // Result: "Hello. I'm Base."

// If method test returns an instance of Derived
instance.test().Hello() // Result: "Hello. I'm Derived."
我不知道这个问题有什么好的解决办法。我刚试过这个:

struct BaseWrapper : Base, wrapper<Base> {
    char const *Hello() {
        printf("BaseWrapper.Hello\n");
        if (override Hello = this->get_override("Hello")) {
            return Hello();
        }
        return Base::Hello();
    }

    char const *default_Hello() {
        printf("BaseWrapper.default_Hello\n");
        return this->Base::Hello();
    }
};

struct DerivedWrapper : Derived, wrapper<Derived> {
    char const *Hello() {
        printf("DerivedWrapper.Hello\n");
        if (override Hello = this->get_override("Hello")) {
            return Hello();
        }
        return Derived::Hello();
    }

    char const *default_Hello() {
        printf("DerivedWrapper.default_Hello\n");
        return this->Derived::Hello();
    }

    Base &test() {
        printf("DerivedWrapper.test\n");
        if (override Hello = this->get_override("test")) {
            return Hello();
        }
        return Derived::test();
    }

    Base &default_test() {
        printf("DerivedWrapper.default_test\n");
        return this->Derived::test();
    }
};
它抛出了一个例外:

TypeError: No to_python (by-value) converter found for C++ type: Base
这篇文章和我的错误相同,但从另一个角度看,它对我帮助不大。

这篇文章解决了一个类似的问题,但也没有帮助我。

我有两个问题:

如果我尝试的方法是正确的,如何解决打字错误问题? 如果我尝试了错误的方法,那么使用boost.python解决问题的最佳方法是什么?
此代码适用于我:

struct Base {
    virtual ~Base() {};
    virtual char const *hello() {
        return "Hello. I'm Base.";
    };
};

struct Derived : Base {
    char const *hello() {
        return "Hello. I'm Derived.";
    };

    Base &test(bool derived) {
        static Base b;
        static Derived d;
        if (derived) {
            return d;
        } else {
            return b;
        }
    }
};

BOOST_PYTHON_MODULE(wrapper)
{
    using namespace boost::python;
    class_<Base>("Base")
        .def("hello", &Base::hello)
        ;

    class_<Derived, bases<Base>>("Derived")
        .def("test", &Derived::test, return_internal_reference<>())
        ;
}

谢谢你的解决方案,它确实解决了我的问题。我还发现手册:
TypeError: No to_python (by-value) converter found for C++ type: Base
struct Base {
    virtual ~Base() {};
    virtual char const *hello() {
        return "Hello. I'm Base.";
    };
};

struct Derived : Base {
    char const *hello() {
        return "Hello. I'm Derived.";
    };

    Base &test(bool derived) {
        static Base b;
        static Derived d;
        if (derived) {
            return d;
        } else {
            return b;
        }
    }
};

BOOST_PYTHON_MODULE(wrapper)
{
    using namespace boost::python;
    class_<Base>("Base")
        .def("hello", &Base::hello)
        ;

    class_<Derived, bases<Base>>("Derived")
        .def("test", &Derived::test, return_internal_reference<>())
        ;
}
>>> import wrapper
>>> d = wrapper.Derived()
>>> d.test(True).hello()
"Hello. I'm Derived."
>>> d.test(False).hello()
"Hello. I'm Base."
>>>