我正在尝试使用 Go 中的 C++ 库。我对 Go 的经验也很少。
我的 C++ 方法如下所示:
std::vector<std::string> getAttribute(const std::string key);
我的.swigcxx
文件中有这个:
%include "std_string.i"
%include "std_vector.i"
%include "MyCode.h"
%template(StringVector) std::vector<std::string>;
%{
#include "MyCode.h"
%}
但是 Go 不想让我访问它。我已经通过以下方式打印了一些信息:
attribs := ipuInfo.GetAttribute("foo")
fmt.Printf("%T\n", attribs)
fmt.Printf("%+v\n", attribs)
我得到:
bar.SwigcptrStringVector
<random number - address perhaps?>
但是我不知道如何获得向量的各个内容。我试过:
for index, val := range bar.GetAttribute("foo") {
}
但这给出了:
cannot range over bar.GetAttribute("foo") (type interface {})
我也试过调用.Capacity()
它的方法,但它没有声明(来自https://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/)。我也试过attribs[0]
,它也不喜欢那样。
有没有人有任何建议?
非常感谢。
看起来是函数重载的问题。我有 2 个getAttribute
具有相同名称和不同参数的函数。当我重命名该方法以使其唯一Capacity()
并Get()
工作时(尽管range
没有)。
看起来 Swig 试图生成包装器代码来调用正确的方法,但无法处理不同的返回类型
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句