猿问

从 Go 迭代 'std::vector<std::string>' ?

我正在尝试使用Go中的C++库。我对围棋也没什么经验。


我的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 {})

我也尝试过调用它的方法,但它没有被声明(从 https://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/)。我也试过,它也不喜欢这样。.Capacity()attribs[0]


有人有什么建议吗?


非常感谢。


汪汪一只猫
浏览 79回答 1
1回答

紫衣仙女

这看起来像是函数重载的问题。我有2个具有相同名称和不同参数的函数。当我重命名该方法,使其唯一且有效(尽管不这样做)时。getAttributeCapacity()Get()range看起来Swig试图生成包装代码来调用正确的方法,但无法处理不同的返回类型。
随时随地看视频慕课网APP

相关分类

Go
我要回答