【问题标题】:Error using std::set to implement a sparse 3D grid使用 std::set 实现稀疏 3D 网格时出错
【发布时间】:2016-09-24 06:59:11
【问题描述】:

我正在尝试使用 std::set 容器实现稀疏 3D 网格,但我无法理解编译器返回的错误,这是我尝试运行的最小示例:

#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;
}

这是编译器错误:

在 /usr/include/c++/4.8/string:48:0 包含的文件中, 来自 /usr/include/c++/4.8/bits/locale_classes.h:40, 来自 /usr/include/c++/4.8/bits/ios_base.h:41, 来自/usr/include/c++/4.8/ios:42, 来自 /usr/include/c++/4.8/ostream:38, 来自 /usr/include/c++/4.8/iostream:39, 从 /home/dede/build/sparse_grid/main.cpp:1: /usr/include/c++/4.8/bits/stl_function.h: 在 'bool std::less<_tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = 单元格]':/usr/include/c++/4.8/bits/stl_tree.h:1324:11:需要来自 '标准::对 std::_Rb_tree<_key _val _keyofvalue _compare _alloc>::_M_get_insert_unique_pos(const key_type&) [with _Key = Cell; _Val = 单元格; _KeyOfValue = std::_Identity; _比较 = std::less; _Alloc = std::allocator; std::_Rb_tree<_key _val>::key_type = Cell]' /usr/include/c++/4.8/bits/stl_tree.h:1377:47:需要来自 'std::pair, bool> std::_Rb_tree<_key _val _keyofvalue _compare _alloc>::_M_insert_unique(_Arg&&) [with _Arg = Cell; _Key = 单元格; _Val = 单元格; _KeyOfValue = std::_Identity; _比较 = std::less; _分配= std::allocator]' /usr/include/c++/4.8/bits/stl_set.h:472:40:
'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 =单元格; _比较 = std::less; _Alloc = std::allocator;类型名 std::_Rb_tree<_key _key std::_identity>, _Compare, 类型名 _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: 从这里需要 /usr/include/c++/4.8/bits/stl_function.h:235:20:错误:传递'const Cell' 作为 'bool Cell::operator* [CMakeFiles/sparse_grid.dir/main.cpp.o] 错误 1 ​​make[1]: * [CMakeFiles/sparse_grid.dir/all] 错误 2 make: *** [all] 错误 2

如果有人能告诉我我做错了什么,我将不胜感激。

谢谢, 费德里科

【问题讨论】:

    标签: c++ stl 3d set


    【解决方案1】:

    您应该将布尔运算符函数声明为const 成员:

    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;}
                                         // ^^^^^
    

    否则这些不能与Cell 的右值对象一起使用。

    【讨论】:

      【解决方案2】:

      您在Cell 中定义了operator &lt;,但错误提示它需要bool std::less&lt;_Tp&gt;::operator()(const _Tp&amp;, const _Tp&amp;) const [with _Tp = Cell]。注意你应该让你的成员函数const

      你可以为less提供一个非成员函数,它可以使用你的成员函数,一旦它是const。

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

      但是,std::less 将为您提供此功能,提供您的成员函数是 const

      【讨论】:

        【解决方案3】:

        您已将 > 和 == 运算符重载的参数声明为 const,并且您正在传递一个临时值。

        只需在循环中创建一个临时的 Cell 对象并将其插入到单元格中

        这样做:

            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);
        }
        

        您的编译应该会成功。

        【讨论】:

        • “你的编译应该成功了。”嗯??
        • 与 OP 的编译器错误没有任何关系。
        • @πάνταῥεῖ 我没有看到你的答案,如果只是看到了你的评论。 :)
        • @πάνταῥεῖ 我认为这是编译器错误,因为模板实例化发生在编译时并且此实例化失败。如果我错了,您是否有其他想法请纠正我。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 2021-01-28
        • 1970-01-01
        • 2023-03-27
        • 2012-09-04
        • 2016-07-26
        相关资源
        最近更新 更多