简体   繁体   English

迭代`std::vector<std::string> ` 来自 Go?</std::string>

[英]Iterate `std::vector<std::string>` from Go?

I'm trying to use a C++ library from Go.我正在尝试使用 Go 中的 C++ 库。 I also have little experience with Go.我对 Go 也没什么经验。

My C++ method looks like this:我的 C++ 方法如下所示:

std::vector<std::string> getAttribute(const std::string key);

I have this in my .swigcxx file:我的.swigcxx文件中有这个:

%include "std_string.i"
%include "std_vector.i"

%include "MyCode.h"

%template(StringVector) std::vector<std::string>;

%{
  #include "MyCode.h"
%}

But Go doesn't want to let me access it.但是 Go 不想让我访问它。 I've printed out some info via:我通过以下方式打印了一些信息:

attribs := ipuInfo.GetAttribute("foo")
fmt.Printf("%T\n", attribs)
fmt.Printf("%+v\n", attribs)

And I get:我得到:

bar.SwigcptrStringVector
<random number - address perhaps?>

But I can't work out how to get to the individual contents of the vector.但我不知道如何获取向量的各个内容。 I've tried:我试过了:

for index, val := range bar.GetAttribute("foo") {
}

But that gives:但这给出了:

cannot range over bar.GetAttribute("foo") (type interface {})

I've also tried calling a .Capacity() method on it but it isn't declared (fromhttps://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/ ).我也尝试过调用.Capacity()方法,但没有声明(来自https://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/ )。 I've also tried attribs[0] and it doesn't like that either.我也尝试过attribs[0] ,但它也不喜欢那样。

Does anyone have any suggestions?有没有人有什么建议?

Many thanks.非常感谢。

It looks like it's a problem with function overloading.看起来这是 function 过载的问题。 I had 2 getAttribute functions with the same name and different parameters.我有 2 个具有相同名称和不同参数的getAttribute函数。 When I rename the method so that it's unique, Capacity() and Get() work (although range doesn't).当我重命名该方法使其唯一时, Capacity()Get()工作(尽管range不工作)。

It looks like Swig tried to generate wrapper code to call the correct method, but couldn't deal with different return types看起来 Swig 试图生成包装器代码来调用正确的方法,但无法处理不同的返回类型

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

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