swig no matching function for overloaded

╄→гoц情女王★ 提交于 2020-01-11 07:34:10

问题


I have a problem in wrapping my c++ code in PHP with SWIG: I have a class in C++ with method which is declared as below:

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

also I include std_string.i in my interface file and I can pass string arguments well. but when I call my method in my PHP code as below:

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

I got this error:

Fatal error: No matching function for overloaded 'PKI_Buf_hexDump'

PKI_Buf is the name of my class. any idea??


回答1:


The problem here seems to be that a typecheck for non-const string references is missing and so during the overload resolution SWIG is rejecting what is really the right candidate to call.

You can work around it by adding your own typecheck, I made an example:

%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";
  }
%}

I'm not too sure what the right precedence value is. I picked 141 because that makes it lower than the default string value.

I checked it all works correctly with:

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

Which worked as expected.

I think the fact that the typecheck typemap for this doesn't work by default is a bug since the typecheck it uses will never work. You might want to raise this on the mailing lists.



来源:https://stackoverflow.com/questions/12331017/swig-no-matching-function-for-overloaded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!