[英]How do I use an array of struct as an index to an array of struct?
I have this array of struct with some operators overloaded 我有一些结构数组,一些运算符被重载
struct xyz
{
int x; float y;
};
std::vector<xyz> a1,a2,a3;
When I use this as 当我用它时
a1 [ a2 [ i ] ] = a3 [ i ]
//by this I mean
//a1 [ a2 [ i ].x ].x = a3 [ i ].x
//a1 [ a2 [ i ].x ].y = a3 [ i ].y
I get this error "\\OCL6D24.tmp.cl", line 236: error: expression must have integral or enum type 我收到此错误“\\ OCL6D24.tmp.cl”,第236行:错误:表达式必须具有整数或枚举类型
I'm using this in an OpenCL kernel. 我在OpenCL内核中使用它。 But this problem is analogous to a normal C++ program.
但是这个问题类似于普通的C ++程序。 How do I solve this?
我该如何解决这个问题?
Update: I don't think what I required is possible, especially in an OpenCL kernel kind of situation. 更新:我不认为我需要的是什么,特别是在OpenCL内核的情况下。 But I solved my issue.
但我解决了我的问题。 It was a design flaw.
这是一个设计缺陷。
You will have to use some kind of associative container to be able to do that. 您将不得不使用某种关联容器才能做到这一点。 For instance
std::map
or std::unordered_map
(on C++11
). 例如
std::map
或std::unordered_map
(在C++11
)。 std::vector
only support indexing using integral types(just like the error says). std::vector
只支持使用整数类型的索引(就像错误所说的那样)。
std::vector::operator[] takes size_t as input, but you are passing an object of xyz
to it. std :: vector :: operator []将size_t作为输入,但是您将
xyz
的对象传递给它。 That's why your compiler rejects your code. 这就是你的编译器拒绝你的代码的原因。
To work around your code, you could overload operator int()
to implicit convert object to integer number: 要解决您的代码,您可以重载
operator int()
以隐式将对象转换为整数:
struct xyz
{
int x; float y;
operator int()
{
return x;
}
};
But you need to make sure the return value relates to correct index in vector. 但是你需要确保返回值与向量中的正确索引相关。
Or use some associative container like std::unordered_map
instead. 或者使用一些像
std::unordered_map
这样的关联容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.