2013-07-22 39 views
1

我的问题是在以下代码:推力与常量源copy_if

过滤器功能编译并运行,因为它应该当源是不恒定(在迭代器被相应调整)。但是,当我将源代码更改为const时,编译器为copy_if语句的前两个变量提供了以下错误: “对象具有与成员函数不兼容的类型限定符”。

我相信有一个const,而不是const转换错误的地方,但坦率地说,我不知道在哪里。任何帮助,将不胜感激。

#include "thrust\device_vector.h" 
#include "thrust\copy.h" 

typedef thrust::device_vector<float>::const_iterator Dc_FloatIterator; 
typedef thrust::device_vector<float>::iterator   D_FloatIterator; 

typedef thrust::device_vector<int>::const_iterator Dc_IntIterator; 
typedef thrust::device_vector<int>::iterator  D_IntIterator; 

typedef thrust::tuple< Dc_IntIterator, Dc_IntIterator, Dc_FloatIterator> Dc_ListIteratorTuple; 
typedef thrust::zip_iterator<Dc_ListIteratorTuple> Dc_ListIterator;//type of the class const iterator 

typedef thrust::tuple< D_IntIterator, D_IntIterator, D_FloatIterator > D_ListIteratorTuple; 
typedef thrust::zip_iterator<D_ListIteratorTuple> D_ListIterator;//type of the class iterator 

struct selector{//selector functor for the copy if call 
const int val; 

selector(int _val) : val(_val) {} 

__host__ __device__ 
bool operator()(const int& x) { 
return (x == val); 
    } 
}; 

class Foo{  
public: 
    thrust::device_vector<int>  ivec1; 
    thrust::device_vector<int>  ivec2; 
    thrust::device_vector<float> fvec1; 

    Foo(){;} 
    ~Foo(){;} 

    D_ListIterator begin(){//cast of begin iterator 
     return D_ListIterator(D_ListIteratorTuple(ivec1.begin(), ivec2.begin(), fvec1.begin())); 
    } 
    D_ListIterator end(){//cast of end iterator 
     return D_ListIterator(D_ListIteratorTuple(ivec1.end(), ivec2.end(), fvec1.end())); 
    } 

    Dc_ListIterator cbegin(){//cast of const begin iterator 
     return Dc_ListIterator(Dc_ListIteratorTuple(ivec1.cbegin(), ivec2.cbegin(), fvec1.cbegin())); 
    } 
    Dc_ListIterator cend(){//cast of const end iterator 
     return Dc_ListIterator(Dc_ListIteratorTuple(ivec1.cend(), ivec2.cend(), fvec1.cend())); 
    } 

    void const_filter(const Foo& TheOther, const int& target){//doesnt work 
     //This function should copy those member of the vectors where 
     //the ivec2[i] == target is true 
     thrust::copy_if(
      TheOther.cbegin(), 
      TheOther.cend(), 
      TheOther.ivec2.cbegin(), 
      this->begin(), 
      selector(target)); 
    } 
    void filter(Foo& TheOther, const int& target){//works 
     //This function should copy those member of the vectors where 
     //the ivec2[i] == target is true 
     thrust::copy_if(
      TheOther.begin(), 
      TheOther.end(), 
      TheOther.ivec2.cbegin(), 
      this->begin(), 
      selector(target)); 
    } 
    void insert(const int& one, const int& two,const float& three){ 
     ivec1.push_back(one); 
     ivec2.push_back(two); 
     fvec1.push_back(three); 
    } 

    int size(){ 
     return ivec1.size(); 
    } 
}; 

bool CheckIfSublistIsConnected(const Foo& list,const int& sublist_num){ 
Foo tmp; 

tmp.const_filter(list, sublist_num); 

return (bool)tmp.size();//for symplicity, othervise here is a function that check if 
         //the edge list represents a connected graph 
} 
int main(void){ 
Foo list; 
bool connected; 
list.insert(10,2,1.0); 
list.insert(11,2,1.0); 
list.insert(12,2,1.0); 
list.insert(10,3,1.0); 
list.insert(10,3,1.0); 

connected=CheckIfSublistIsConnected(list,2); 

if(connected) return 0; 
else return -1; 
} 

我发现,与如下因素编译器更换TheOther.cbegin()/.cend()接受它。这意味着我搞砸了typedef部分的某处,但是在哪里?

thrust::make_zip_iterator(
     thrust::make_tuple(
      TheOther.ivec1.cbegin(), 
      TheOther.ivec2.cbegin(), 
      TheOther.fvec1.cbegin())) 
+0

您能否提供一个完整的例子,显示在传递/编译情况下调用'Foo :: filter',以及调用'Foo :: filter'的参数会导致编译失败? –

+0

这并不完全。我不知道'cbegin()'和'cend()'是什么样子。你能创建一个简单的,可编译的例子来证明这个问题,但是没有任何不必要的代码来证明这个问题吗?我希望能够复制,粘贴和编译一些内容,而无需编辑任何内容或添加任何内容。 –

+0

我编辑了一个显示问题的代码,您可以尝试编译它。在构建这个示例时,我注意到如果过滤器(...)调用只接受Foo&(而不是const),并且我调用cend和cbegin,它就可以工作。然而,在我的真实程序中,这个调用被深度调用了5次,第一个函数也将Foo作为const引用,所以我必须将它作为const引用传递。 – Slawo

回答

2

因为它出来了我frogotten添加常量魔法字在cend/cbegin的定义。

Dc_ListIterator cbegin() const { 
     return Dc_ListIterator(Dc_ListIteratorTuple(ivec1.cbegin(), ivec2.cbegin(), fvec1.cbegin())); 
    } 
Dc_ListIterator cend() const { 
     return Dc_ListIterator(Dc_ListIteratorTuple(ivec1.cend(), ivec2.cend(), fvec1.cend())); 
    }