简体   繁体   中英

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

I'm using this in an OpenCL kernel. But this problem is analogous to a normal C++ program. How do I solve this?

Update: I don't think what I required is possible, especially in an OpenCL kernel kind of situation. 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::vector only support indexing using integral types(just like the error says).

std::vector::operator[] takes size_t as input, but you are passing an object of xyz to it. 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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