Cuda uint2型推力矢量:“1”;没有成员x“;编译器错误?

Cuda uint2型推力矢量:“1”;没有成员x“;编译器错误?,cuda,thrust,Cuda,Thrust,我刚刚开始使用推力库。我试图在设备上制作一个长度为5的向量。我正在设置第一个元素的成员vec[0] #include<thrust/device_vector.h> #include<iostream> . . . thrust::device_vector<uint2> vec(5); vec[0]=make_uint2(4,5); std::cout<<vec[0].x<<std::endl;

我刚刚开始使用推力库。我试图在设备上制作一个长度为5的向量。我正在设置第一个元素的成员
vec[0]

  #include<thrust/device_vector.h>
  #include<iostream>
  .
  . 
  . 

  thrust::device_vector<uint2> vec(5);
  vec[0]=make_uint2(4,5);
  std::cout<<vec[0].x<<std::endl;
#包括
#包括
.
. 
. 
推力:装置_向量向量向量向量(5);
vec[0]=make_uint2(4,5);

std::cout正如Talonmes在他的评论中指出的那样,您不能直接访问
设备向量
所拥有的元素的成员,或者使用
设备引用
包装的任何对象。然而,我想提供这个答案来演示解决您的问题的另一种方法


尽管
device\u reference
不允许您访问包装对象的成员,但它与
操作符兼容。设备向量不支持您尝试的直接访问方式。对主机变量使用单独的赋值语句,然后打印主机变量。@Talonmes似乎“x”仍然无效。。。更完整的例子?@talonmies谢谢你。它现在起作用了。看起来推力只能从设备打印ints float和DOUBLE之类的基本数据类型,而不能打印任何向量数据类型,如
uint2
@smilingfounda Update:)可能的重复
error: class "thrust::device_reference<uint2>" has no member "x"

1 error detected in the compilation of "/tmp/tmpxft_000020dc_00000000-4_test.cpp1.ii".
#include <thrust/device_vector.h>
#include <iostream>

// provide an overload for operator<<(ostream, uint2)
// as one is not provided otherwise
std::ostream &operator<<(std::ostream &os, const uint2 &x)
{
  os << x.x << ", " << x.y;
  return os;
}

int main()
{
  thrust::device_vector<uint2> vec(5);
  vec[0] = make_uint2(4,5);
  std::cout << vec[0] << std::endl;
  return 0;
}