2016-05-26 4 views
0

std :: setコンテナでスパースな3Dグリッドを実装しようとしていますが、コンパイラから返されたエラーを理解できません。誰かが私が間違ってやっているものを私に言うことができる場合、私は本当に感謝std :: setを使ってスパースな3Dグリッドを実装する際にエラーが発生する

In file included from /usr/include/c++/4.8/string:48:0, from /usr/include/c++/4.8/bits/locale_classes.h:40, from /usr/include/c++/4.8/bits/ios_base.h:41, from /usr/include/c++/4.8/ios:42, from /usr/include/c++/4.8/ostream:38, from /usr/include/c++/4.8/iostream:39, from /home/dede/build/sparse_grid/main.cpp:1: /usr/include/c++/4.8/bits/stl_function.h: In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Cell]': /usr/include/c++/4.8/bits/stl_tree.h:1324:11: required from 'std::pair std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = Cell; _Val = Cell; _KeyOfValue = std::_Identity; _Compare = std::less; _Alloc = std::allocator; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::key_type = Cell]' /usr/include/c++/4.8/bits/stl_tree.h:1377:47: required from 'std::pair, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_Arg&&) [with _Arg = Cell; _Key = Cell; _Val = Cell; _KeyOfValue = std::_Identity; _Compare = std::less; _Alloc = std::allocator]' /usr/include/c++/4.8/bits/stl_set.h:472:40:
required from 'std::pair, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(std::set<_Key, _Compare, _Alloc>::value_type&&) [with _Key = Cell; _Compare = std::less; _Alloc = std::allocator; typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator; std::set<_Key, _Compare, _Alloc>::value_type = Cell]' /home/dede/build/sparse_grid/main.cpp:53:57: required from here /usr/include/c++/4.8/bits/stl_function.h:235:20: error: passing 'const Cell' as 'this' argument of 'bool Cell::operator<(const Cell&)' discards qualifiers [-fpermissive] { return __x < __y; } ^make[2]: * [CMakeFiles/sparse_grid.dir/main.cpp.o] Error 1 make[1]: * [CMakeFiles/sparse_grid.dir/all] Error 2 make: *** [all] Error 2

:私が実行しようとしている:

#include <iostream> 
#include <vector> 
#include <limits> 
#include <set> 

#include <Eigen/Core> 

using namespace std; 

class Cell { 
public: 
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW 
    Cell(const Eigen::Vector3i idx=Eigen::Vector3i::Zero()):_idx(idx) { 
     _center = Eigen::Vector3f::Zero(); 
     _parent = 0; 
     _distance = std::numeric_limits<int>::max(); 
    } 

    inline bool operator < (const Cell& c){ 
     for (int i=0; i<3; i++){ 
      if (_idx[i]<c._idx[i]) 
       return true; 
      if (_idx[i]>c._idx[i]) 
       return false; 
     } 
     return false; 
    } 

    inline bool operator == (const Cell& c) { return c._idx == _idx;} 

private: 
    Eigen::Vector3i _idx; 
    Eigen::Vector3f _center; 
    vector<Eigen::Vector3f> _points; 
    Cell* _parent; 
    size_t _closest_point; 
    float _distance; 
    int _tag; 
}; 


int main(int argc, char* argv[]) { 

    set<Cell> grid; 

    float max = 1, min = -1; 
    int dim = 5; 
    float delta = (max-min)/(dim-1); 

    for(int k = 0; k < dim; k++) 
     for(int j = 0; j < dim; j++) 
      for(int i = 0; i < dim; i++) 
       grid.insert(Cell(Eigen::Vector3i(i,j,k))); 

    return 0; 
} 

を、これはコンパイラエラーです。

おかげで、 フェデリコ

答えて

1

あなたはconstメンバーとして、あなたのブール演算子関数を宣言する必要があります。そうでなければ、これらはCellの右辺値オブジェクトで使用することはできません

inline bool operator < (const Cell& c) const { 
            // ^^^^^ 
    for (int i=0; i<3; i++){ 
     if (_idx[i]<c._idx[i]) 
      return true; 
     if (_idx[i]>c._idx[i]) 
      return false; 
    } 
    return false; 
} 

inline bool operator == (const Cell& c) const { return c._idx == _idx;} 
            // ^^^^^ 

+0

どうもありがとうございました、これは:)私の問題を解決しました) –

0

あなたはCelloperator <を定義しましたが、エラーは、それがbool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Cell]を望んでいると言います。メンバーの機能をconstにする必要があります。

あなたはそれがconstのになると、お使いのメンバ関数を使用することができ、lessのために非メンバ関数を提供することができます。

bool operator <(const Cell &a, const Cell &b) 
{ 
    return a < b; 
} 

しかし、std::lessあなたのメンバ関数がconstで提供する、あなたのためにこれを提供します。

0

>および==演算子のオーバーロードがconstとして宣言し、一時的に渡しています。

ちょうどこのようにそれを実行したループ内のセルの一時的なオブジェクトを作成し、セルに

それを挿入します。

for(int k = 0; k < dim; k++) 
     for(int j = 0; j < dim; j++) 
      for(int i = 0; i < dim; i++) 
{ 
      Eigen::Vector3i(i,j,k) eigenVec; 
      Cell cell(eigenVec); 
      grid.insert(cell); 
} 

あなたのコンパイルが成功しなければなりません。

+0

_「あなたのコンパイルが成功しなければならない。」_はぁ? –

+0

OPのコンパイラエラーとは何の関係もありません。 –

+0

@πάνταῥεῖ私はあなたの答えを見たことがありません。 :) –

関連する問題