c++11type-traitsxubuntugcc4.8

C++ error on std::unordered_map (Ubuntu - GCC4.8.2)


I'm trying to compile someone else project on linux (Ubuntu), it's a game that uses SDL2. I'm compiling with Code::Blocks using GCC4.8.2 and with the C++11 flag. I spend the las hours to look in internet for the error or even understand it, but with no luck at all. I hope anyone can help me or at least give me a lead.

The error is:

/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

And the code for 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

And, if it helps, the code for 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

Thanks in advance!!!!


Solution

  • You have this

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

    a map where the key is a Position value, then the hash function

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

    taking a Poisition pointer.

    The map will pass the key (a Position value) to the hash-function, but the hash-function doesn't take a value (or reference), it takes a pointer, and that's where the error comes from. Change the hash-function to take a reference instead:

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