【问题标题】:C++ error on std::unordered_map (Ubuntu - GCC4.8.2)std::unordered_map 上的 C++ 错误(Ubuntu - GCC4.8.2)
【发布时间】:2015-01-29 09:04:00
【问题描述】:

我正在尝试在 linux (Ubuntu) 上编译其他项目,这是一个使用 SDL2 的游戏。我正在使用 GCC4.8.2 和 C++11 标志使用 Code::Blocks 进行编译。我花了几个小时在互联网上查找错误甚至理解它,但一点运气都没有。我希望任何人都可以帮助我,或者至少给我一个领导。

错误是:

/usr/include/c++/4.8/bits/hashtable_policy.h: In instantiation of ‘struct std::__detail::__is_noexcept_hash<Position, PositionHash>’:
/usr/include/c++/4.8/type_traits:121:12:   recursively required from ‘struct std::__and_<std::is_default_constructible<PositionHash>, std::is_copy_assignable<PositionHash>, std::__detail::__is_noexcept_hash<Position, PositionHash> >’
/usr/include/c++/4.8/type_traits:121:12:   required from ‘struct std::__and_<std::__is_fast_hash<PositionHash>, std::is_default_constructible<PositionHash>, std::is_copy_assignable<PositionHash>, std::__detail::__is_noexcept_hash<Position, PositionHash> >’
/usr/include/c++/4.8/type_traits:127:38:   required from ‘struct std::__not_<std::__and_<std::__is_fast_hash<PositionHash>, std::is_default_constructible<PositionHash>, std::is_copy_assignable<PositionHash>, std::__detail::__is_noexcept_hash<Position, PositionHash> > >’
/usr/include/c++/4.8/bits/unordered_map.h:99:66:   required from ‘class std::unordered_map<Position, SDLGameObject*, PositionHash>’
/home/*/*/games/magictower/Magic-Tower/Floor.h:17:61:   required from here

以及 Floor.h 的代码:

#ifndef __FLOOR_H__
#define __FLOOR_H__
#include <vector>
#include <unordered_map>


#include "Position.h"

//class SDLGameObject;
#include "SDLGameObject.h"

// Floor stores elements on that floor

class Floor {

public:
    std::unordered_map<Position, SDLGameObject*, PositionHash> elements;  // Line of the error
    std::vector<std::vector<SDLGameObject*> > map;// Map

public:
    Floor();
    ~Floor();

    void render();
    void cleanUp();
};
#endif

而且,如果有帮助,Position.h 的代码:

#ifndef __POSITION_H__
#define __POSITION_H__

class Position {
public:
    int x_coord;
    int y_coord;

public:
    Position(int x, int y):x_coord(x),y_coord(y){}

    bool operator==(const Position& other) const {
     return x_coord == other.x_coord &&
            y_coord == other.y_coord;
    }

};

class PositionHash {

public:
    std::size_t operator()(const Position* p) const {
         // 16 is the maximum value in the pair of coordination integers
         return p->x_coord * 16 + p->y_coord;
    }
};

#endif

提前致谢!!!!

【问题讨论】:

    标签: c++11 typetraits xubuntu gcc4.8


    【解决方案1】:

    你有这个

    std::unordered_map<Position, SDLGameObject*, PositionHash>
    

    一个map,其中key是Positionvalue,然后是hash函数

    std::size_t operator()(const Position* p) const
    

    获取Poisition指针

    映射会将键(Position 值)传递给哈希函数,但哈希函数不接受值(或引用),它接受指针,这就是错误的来源.更改散列函数以改为引用:

    std::size_t operator()(const Position& p) const
    

    【讨论】:

    • 有道理...感谢所有解释。稍后我会在家里测试它,非常感谢。我会告诉你它是怎么回事!
    • 效果很好,PositionHash的最终代码是:std::size_t operator()(const Position& p) const,如你所说,返回部分:return p.x_coord * 16 + p .y_coord;谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2013-11-05
    相关资源
    最近更新 更多