【问题标题】:swig no matching function for overloadedswig 没有重载的匹配函数
【发布时间】:2012-09-08 13:12:08
【问题描述】:

我在使用 SWIG 将我的 c++ 代码包装在 PHP 中时遇到问题: 我在 C++ 中有一个类,其方法声明如下:

int hexDump(string &dmpstr,bool space=true)const;

我还在我的接口文件中包含 std_string.i 并且我可以很好地传递字符串参数。 但是当我在我的 PHP 代码中调用我的方法时,如下所示:

$bf->hexDump('12',true);

我收到了这个错误:

Fatal error: No matching function for overloaded 'PKI_Buf_hexDump'

PKI_Buf 是我的班级名称。 有什么想法吗??

【问题讨论】:

  • PS:hexDump 函数是一个重载函数,它也用 4 个参数声明为:static int hexDump(byte *inbuf, int sz, string &dmpstr, bool space=true);跨度>

标签: php function swig


【解决方案1】:

这里的问题似乎是缺少非常量字符串引用的类型检查,因此在重载解决方案期间,SWIG 拒绝了真正合适的调用候选者。

你可以通过添加自己的类型检查来解决它,我做了一个例子:

%module test

%include <std_string.i>

%typemap(typecheck,precedence=141) std::string& str {
  $1 = Z_TYPE_PP($input) == IS_STRING;
}

%{
#include <iostream>
%}

%inline %{
  void func(std::string& str, bool b=false) {
    std::cout << "In C++: " << str << "\n";
    str = "output";
  }
%}

我不太确定precedence value 是什么权利。我选择 141 是因为它低于默认字符串值。

我检查了它是否正常工作:

<?php
include('test.php');
echo "testing\n";
$str = "input";
test::func($str, true);
echo "In PHP: " . $str . "\n";
?>

按预期工作。

我认为默认情况下类型检查类型映射不起作用的事实是一个错误,因为它使用的类型检查永远不会起作用。您可能想在邮件列表中提出这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-24
    • 2021-04-19
    • 2015-06-20
    • 2014-08-11
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多