错误 C2064 使用 <functional> 和<bind>
Error C2064 using <functional> and <bind>
我真的不知道该怎么做。我查找的每个答案都有我不理解的语法。
错误:
Error 1 error C2064: term does not evaluate to a function taking 1 arguments
我在哈希表构造函数中使用函数指针。有人建议我使用 和 标题来解决我遇到的问题。它解决了错误,但我遇到了上述错误。
我的哈希表声明和 ctor 如下:
#pragma once
#include "SLList.h"
template<typename Type> class HTable
{
public:
HTable(unsigned int numOfBuckets, std::function<unsigned int(const Type&)> hFunction);
~HTable();
HTable<Type>& operator=(const HTable<Type>& that);
HTable(const HTable<Type>& that);
void insert(const Type& v);
bool findAndRemove(const Type& v);
void clear();
int find(const Type& v) const;
private:
SLList<Type>* ht;
std::function<unsigned int(const Type&)> hFunct;
unsigned int numOfBuck;
};
template<typename Type>
HTable<Type>:: HTable(unsigned int numOfBuckets, std::function<unsigned int(const Type&)> hFunction)
{
ht = new SLList<Type>[numOfBuckets];
this->numOfBuck = numOfBuckets;
this->hFunct = hFunction;
}
Game.h(包含表格):
#pragma once
#include "stdafx.h"
#include "HTable.h"
#include "BST.h"
#include "DTSTimer.h"
using namespace std;
class Game
{
public:
Game(void);
virtual ~Game(void);
void refresh();
void input();
unsigned int xorHash(const string &s);
private:
string userInput;
DTSTimer timer;
BST<string> answers;
HTable<string> dictionary;
};
游戏.cpp(我正在尝试传入 xorHash 函数)
#include "Game.h"
Game::Game(void) : dictionary(2048, std::bind(&Game::xorHash, this))
{
}
Game::~Game(void)
{
}
void Game::refresh()
{
}
void Game::input()
{
}
unsigned int Game::xorHash(const string &s)
{
return 0;
}
提前谢谢。
你需要一个占位符来表示未绑定函数参数:
std::bind(&Game::xorHash, this, std::placeholders::_1)
根据口味,lambda 可能更具可读性:
[this](const std::string & s){return xorHash(s);}
虽然我不清楚为什么xorHash
需要成为非静态成员;当然,哈希应该只依赖于它的输入?
xorHash
是一个接受 1 个参数的方法。 这意味着它也隐式地需要一个this
指针。
使其成为static
方法或class
之外的自由函数。
要传递的哈希函数对象仍然需要将哈希值作为参数。也就是说,您要绑定类似的东西
std::bind(&Game::xorHash, this, std::placeholders::_1)
需要_1
-bit来告诉std::bind()
参数必须去哪里以及将哪个参数发送到那里(在这种情况下,哪一个不是那么有趣,因为只有一个;如果你绑定要接收多个参数的函数,它会更有趣)。
请注意,实际上你不太可能想要传递一个真正的成员函数:通常,计算的哈希值不依赖于对象状态,也就是说,你可能最好xorHash()
类的static
成员函数并传递这个:这样你甚至不需要std::bind()
任何参数。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中