ark
ark

Reputation: 3

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.

Upvotes: 0

Views: 228

Answers (2)

billz
billz

Reputation: 45410

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.

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70931

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).

Upvotes: 1

Related Questions