小编Fab*_*ian的帖子

"C++库不会为这种类型提供哈希值." - 在std :: unordered_map中使用自己的类

我正在尝试编写康威的"生命游戏".在接近我的目标时,我遇到了编译错误:

C2338:C++库不提供此类型的哈希值.

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

我的问题是:
是否可以使用我自己的类使用自己的hashCode方法std::unordered_map?我需要使用一个可以容纳两个数字的类.(我也尝试过std::tuple,但是struct东西).

这是我的一张代码:

#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 >= …
Run Code Online (Sandbox Code Playgroud)

c++ unordered-map sfml

2
推荐指数
1
解决办法
62
查看次数

标签 统计

c++ ×1

sfml ×1

unordered-map ×1