简体   繁体   English

将二维向量数组传递给 class 构造函数

[英]Passing a 2d array of vector to a class constructor

In my code, I have some data stored in an 2d array of vector and I'd like to use it in another class to process the data but I can't find a way to do that.在我的代码中,我有一些数据存储在一个二维向量数组中,我想在另一个 class 中使用它来处理数据,但我找不到这样做的方法。

Here is a sample of what I tried:这是我尝试过的示例:

vector<IndividualData> vecArray[5][5]; // The one I want to pass to the class

MyClass::MyClass(vector<IndividualData> data[][5], ...):_data(data)

Is there a good way to do that?有什么好的方法吗?

I also tried with the std::array but I can't make it work:我也尝试使用 std::array 但我无法使其工作:

array<array<vector<IndividualData>, 5>, 5> testVec; // Ther one I want to pass to the class

MyClass::MyClass(array<array<vector<IndividualData>, 5>, 5>& data, ...): _data(data)                                                                                      

When I want to use it in the class, I get the following error:当我想在 class 中使用它时,我收到以下错误:

Type 'array<array<vector, 5>, 5>' does not provide a subscript operator类型 'array<array<vector, 5>, 5>' 不提供下标运算符

With

printf("%d\r\n", _data[0][0][0].param);

and

array<array<vector<IndividualData>, 5>, 5>& _data;

Thank's in advance for your replies提前感谢您的回复

This works for me:这对我有用:

using My2DArrayOfVec = std::array<std::array<std::vector<IndividualData>, 5>, 5>;

class MyClass
{
   My2DArrayOfVec _data;
public:
   MyClass(My2DArrayOfVec const& data, ...) : _data(data) {}
   void useIt() const { printf("%d\r\n", _data[0][0][0].param); }
};

By having an alias, I always use the same types, My2DArrayOfVec , inside and outside of my class even when I'm experimenting.通过使用别名,我总是在我的 class 内部和外部使用相同的类型My2DArrayOfVec ,即使我正在试验。

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

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