0

我正在尝试在 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

提前致谢!!!!

4

1 回答 1

0

你有这个

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

键是Position 的映射,然后是哈希函数

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

拿一个Poisition 指针

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

std::size_t operator()(const Position& p) const
于 2015-01-29T09:15:46.507 回答