Structures w/thrust::host_vector usage?

Is it possible to declare variables within structures as follows:

typedef struct my_struct_1 {

thrust::host_vector h_var(2);

} MYSTRUCT_1

and then be able to access them?

I defined this structure in a header file.

In my source code, I did the following:

MYSTRUCT_1 *sgc = new MYSTRUCT_1;
memset(sgc, 0, sizeof MYSTRUCT_1);

sgc->h_var[0] = 14.0;
sgc->h_var[1] = 24.0;

When I define h_var as shown above and then compile, I get the following error related to my header file at the line where I made the ‘thrust::’ specification:

error: expected identifier before numeric constant
error: expected ‘,’ or ‘…’ before numeric constant

In the source code where I assign a value of 14.0 to h_var[0] and 24.0 to h_var[1] I get this error for both assignments:

error: invalid types ‘< unresolved overloaded function type >[int]’ for array subscript

If I replace the 'thrust::host_vector declaration above with just ‘float h_var[2]’, I have no problems compiling and executing.

Have I made an error along the way somewhere, or can I even utilize the thrust capabilities within a C++ structure?

Here’s how to do what you want:

#include <thrust/host_vector.h>

struct my_struct_1

{

  thrust::host_vector<float> h_var;

};

int main(void)

{

  my_struct_1 sgc;

sgc.h_var.resize(2);

  sgc.h_var[0] = 14.0;

  sgc.h_var[1] = 24.0;

return 0;

}

Your code which does

thrust::host_vector<float> h_var(2);

inside the definition of MYSTRUCT_1 doesn’t make sense because host_vector dynamically allocates its memory, unlike a static array of floats. That’s why your code which does “float h_var[2]” works but fails with host_vector’s constructor.

Note that your code which does

memset(sgc, 0, sizeof(MYSTRUCT_1));

Is in general unsafe without knowledge of the implementation details of MYSTRUCT_1 and its members (such as host_vector).