繁体   English   中英

python编译错误(绑定c ++)

[英]python compile error (binding c++)

我正在尝试在我的c ++模拟中实现数组(提供的编码是在一个网络数据包字段中具有2个值)

这是我的变量声明(在标题中)

 Ptr<Name> m_names [2];

这是我的函数声明(在标题中)

void SetName (unsigned which, Ptr<Name> name);
void SetName (unsigned which, const Name &name);

在源文件中

void Interest::SetName (unsigned which, Ptr<Name> name)
{
    if (which < 2)
    {
        m_names[which] = name;
    }
}

void Interest::SetName (unsigned which, const Name &name)
{
    if (which < 2)
    {
        m_names[which] = Create<Name> (name);
    }
}

我这样称呼的方式是这样的(在我的主文件中):

interest->SetName (0, nameWithSequence);
interest->SetName (1, nameWithNextSequence);

结果它给出了这样的错误

src/ndnSIM/bindings/ns3module.cc: In function ‘PyObject* _wrap_PyNs3NdnData_SetName__0(PyNs3NdnData*, PyObject*, PyObject*, PyObject**)’:
src/ndnSIM/bindings/ns3module.cc:8418:62: error: no matching function for call to ‘ns3::ndn::Data::SetName(ns3::Ptr<ns3::ndn::Name>)’
src/ndnSIM/bindings/ns3module.cc:8418:62: note: candidates are:
./ns3/ndn-data.h:60:3: note: void ns3::ndn::Data::SetName(unsigned int, ns3::Ptr<ns3::ndn::Name>)
./ns3/ndn-data.h:60:3: note:   candidate expects 2 arguments, 1 provided

问题是我应该声明正确的陈述的正确方法是什么。 感谢任何帮助

编辑我发现了一些pyhton定义来绑定我的c ++代码(SetName)

def __setattr__(self, name, value):
    if name == "_interest":
        return object.__setattr__ (self, name, value)

    elif name == "name":
        if value is None:
            return self._interest.SetName (ns.ndnSIM.ndn.Name ())
        elif isinstance (value, Name):
            return self._interest.SetName (value._name)
        elif isinstance (value, ns.ndnSIM.ndn.Name):
            return self._interest.SetName (value)
        elif isinstance (value, str):
            return self._interest.SetName (ns.ndnSIM.ndn.Name (value))
        else:
            raise ValueError ("Invalid name parameter")

解决此问题的正确方法以及方法是什么。 谢谢

好吧..我只举一个例子。 我假设Ptr行为类似于std::shared_pointerName类似于std::string (至少出于示例目的)。

首先,我建议使用std::array而不是C样式的数组。 但这并不是必需的。 第二。 如果将m_names计划为fidex大小的数组,则在访问不存在的成员时可以引发异常。

这是一些代码。
标头(test.h):

#include <string>
#include <memory>
#include <array>

typedef std::shared_ptr<std::string> pString;

struct A{
protected:
    static constexpr size_t array_size = 2;
    std::array<pString, array_size> m_names;
    static void CheckRange(size_t);
public:
    void SetName(size_t id, const std::string& name);
    void SetName(size_t id, const pString& name);

    const std::string& GetName(size_t id) const;
    const pString& GetNamePtr(size_t id) const;
};

资源:

#include "test.h"
#include <exception>

void A::SetName(size_t id, const pString &name){
    CheckRange(id);
    m_names[id] = name;
}

void A::SetName(size_t id, const std::string& name){
    CheckRange(id);
    m_names[id] = std::make_shared<std::string>(name);
}

const std::string& A::GetName(size_t id) const{
    CheckRange(id);
    if (!m_names[id])
            throw std::logic_error("Pointer is not initialized");
    return *(m_names[id].get());
}

const pString& A::GetNamePtr(size_t id) const {
    CheckRange(id);
    return m_names[id];
}

这是CheckRange函数的示例:

void A::CheckRange(size_t id){
    if (!(id < array_size))
            throw std::logic_error("ID should be < " +
              std::to_string(array_size));
}

此代码经过一些测试: http : //ideone.com/rUvVhH

暗示

if (m_names[id])检查m_names[id]包含有效的指针(例如,不包含nullptr )。 我想Ptr具有一些类似的功能。 而且我相信std::make_shared<T>(...)Create<T>(...)类似。

好吧,对我而言,这或多或少是检索数组内容的正确方法:)
如果事情太复杂,我将尝试使其更容易理解。
欢迎您询问是否有任何疑问!

该错误是在Python绑定代码中的编译时发生的。 所以,

  1. 如果您自己编写了绑定(包装器)代码(使用Python C API或boost :: python),则必须找到定义SetName绑定的位置,它将丢失参数。

  2. 如果绑定是由某些工具(例如SWIG或SIP)自动生成的,则必须找到SetName绑定的定义位置:这是包装器工具用来确定要生成的代码的地方,这是该定义中的错误。

您添加到帖子setattr的Python代码清楚地表明self._interest.SetName(obj)调用是错误的:您的C ++显示SetName具有两个参数。 如果setattr是由某些工具(ns3?)生成的,则必须查找如何执行此操作以为其赋予SetName正确的定义,以便setattr具有正确的调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM