gpt4 book ai didi

c++ - 错误 : type/value mismatch at argument 1 in template parameter list for ‘template class std::unique_ptr’

转载 作者:行者123 更新时间:2023-11-30 04:56:18 28 4
gpt4 key购买 nike

在我们开始之前,我完全是 C++11 的菜鸟,几年前就用过 C。

我正在尝试使用 pybind11 编写 C++11 代码的 python 绑定(bind),但出现了subjected 错误。我基本上是在关注这个 guide来自 Nvidia 的人,并被困在这个错误中。有好心人能给我指出正确的方向吗?

定义:

template<int zoom_factor>
class UpSamplePlugin: public nvinfer1::IPluginExt
{
public:
UpSamplePlugin() {}

// Create the plugin at runtime from a byte stream.
UpSamplePlugin(const void* buffer, size_t size)
{
assert(size == sizeof(mInputDims)); // assert datatype of input
mInputDims = *reinterpret_cast<const nvinfer1::Dims*>(buffer);
}
...
}

调用:

py::class_<UpSamplePlugin, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;

错误:

/media/.../plugin/pyUpSample.cpp: In function ‘void pybind11_init_upsampleplugin(pybind11::module&)’:
/media/.../plugin/pyUpSample.cpp:13:90: error: type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr’
py::class_<UpSamplePlugin, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
^
/media/.../plugin/pyUpSample.cpp:13:90: note: expected a type, got ‘UpSamplePlugin’

最佳答案

没有名为 UpSamplePlugin 的类型,这只是一个模板。所以你必须做类似 UpSamplePlugin<T> 的事情.在您的情况下,它应该是 UpSamplePlugin<zoom_factor>

尝试以下代码,如果此声明在模板内:

py::class_<UpSamplePlugin<zoom_factor>, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;

编译器将“创建”一个对应于 UpSamplePlugin<zoom_factor> 的新类型.

如果不在模板内:

创建另一个模板(它可以是一个模板函数),可以用 zoom_factor 调用它是任何常量类型:

template<int zoom_factor>
void doSomething() {
py::class_<UpSamplePlugin<zoom_factor>, nvinfer1::IPluginExt, std::unique_ptr<UpSamplePlugin, py::nodelete>>(m, "UpSamplePlugin")
// Bind the normal constructor as well as the one which deserializes the plugin
//.def(py::init<const nvinfer1::Weights*, int>())
.def(py::init<const void*, size_t>())
;
}

然后您可以使用任何已知编译时间调用此函数 zoom_factor

关于c++ - 错误 : type/value mismatch at argument 1 in template parameter list for ‘template<class _Tp, class _Dp> class std::unique_ptr’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52682145/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com