" C ++ Library为这种类型提供了哈希。" - 在std :: unordered_map中使用自己的类

时间:2017-05-14 12:48:59

标签: c++ sfml unordered-map

我正在尝试编写Conway" Life of Life"。在接近我的目标时,我遇到了编译错误:

  

C2338:C ++ Library doesen没有提供此类型的哈希值。

起初我使用了SFML类sf::Vector2D。当它无法为我工作时,我写了一个属于我自己的类,希望我能实现丢失的hashCode方法。

我的问题是:
是否可以使用我自己的类及其hashCode方法std::unordered_map?我需要使用一个可以容纳两个数字的类。 (我也尝试了std::tuplestruct和其他内容。

这是我的一张代码:

#include "GameMechanics.h"



GameMechanics::GameMechanics(Elements * elements):elements(elements)
{
    this->refreshTime = 1000000;    //ms
    this->clock.restart();
}


GameMechanics::~GameMechanics()
{
}

bool GameMechanics::isRunning()
{
    return this->running;
}

void GameMechanics::setRunning(bool running)
{
    this->running = running;
}

void GameMechanics::loop()
{

    unsigned passedTime = clock.getElapsedTime().asMicroseconds();  //check passed time since the clock got restarted
    this->timeHeap  +=  passedTime; //add passed time to the timeheap
    this->clock.restart();
    //only refresh every "refreshTime" seconds
    if (timeHeap >= this->refreshTime) {    
        std::cout << "Calculated new generation!" << std::endl;
        this->timeHeap -= this->refreshTime;
        this->calculateNextGeneration();
    }
}

void GameMechanics::calculateNextGeneration()
{
    std::list<sf::Vector2i> oldGeneration = this->elements->getElements();  //  population in the moment

    sf::Vector2u elements = this->elements->getElementCount();

    std::unordered_map<MyVector2D, int> counter;     //here is the problem. Thats the line that makes some trouble


    for (std::list<sf::Vector2i>::iterator it = oldGeneration.begin(); it != oldGeneration.end(); it++) {
        sf::Vector2i position = *it;

        for (int i = -1; i < 2; i++) 
        {
            for (int j = -1; j < 2; j++) 
            {
                if (position.x + i >= 0 && position.x + i <= this->elements->getElementCount().x &&
                    position.y + j >= 0 && position.y + j <= this->elements->getElementCount().y) 
                {
                    if (counter.find(MyVector2D(position.x + i, position.y + j)) != counter.end()) 
                    {
                        counter.at(MyVector2D(position.x + i, position.y + j))++;
                    }
                    else //if there is no such element, create a new entry
                    {
                        counter.insert({ MyVector2D(position.x + i, position.y + j),1 });
                    }
                }
            }
        }
    }


    //create new generation
    this->brithNewGeneration(&counter);
}

void GameMechanics::brithNewGeneration(std::unordered_map<MyVector2D,int>* counter)
{
    //this methode does work

    std::list<sf::Vector2i> newGeneration;
//  for (std::unordered_map<MyVector2D, int>::iterator it = counter->begin(); it != counter->end(); it++) 
    {
        //if life vell with < 2 neighbours, it dies
        //life cell with 2 or 3 neighbours will continue living

        //life cell with >4 cells will die

        //dead cell with 3 neighbours will start living
    }
}

1 个答案:

答案 0 :(得分:6)

std::unordered_map(和std::unordered_set)所需的自定义散列函数不是存储类型的成员函数。您需要专门化std::hash模板:

namespace std {
    template<>
    struct hash<YourType> {
        using argument_type = YourType;
        using result_type = std::size_t;

        result_type operator()(argument_type const &obj) const {
            // Compute and return the hash value for `obj`.
        }
    };
}

你的情况正是这样做的原因:如果你愿意的话,你可以std::hash专注sf::Vector2D,不需要实现你自己的类。