c++ - 测试完美哈希函数时出现超出范围错误
问题描述
我正在尝试测试我的哈希项目的功能之一。此函数的目标是为 1000 个字符串列表中的字符串创建索引。它应该将字符串中每个字符的 ASCII 值相加,将其乘以一个随机数 (int c),然后将其与一个素数 (int prime1) 取模,以找到哈希中的索引。我正在尝试使用字符串“hi”测试此代码,但每当我这样做时,我都会不断收到函数超出范围错误。我不明白为什么,因为主要功能就在它试图测试的功能之下。
这是我的课
//using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
// Public Member functions
public:
hash(); // Constructor
int CreateG(const std::string city);
int HashFunction(std::string city); // the declaration of the hash function for the primary hash
// Private member functions
private:
// Protected Member functions
protected:
}
#endif
这是我的 .cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "Hash.h"
//using namespace std;
int hash::HashFunction(std::string city){ // the declaration of the hash function for the primary hash
}
int hash::CreateG(const std::string city){ // creates g(str) to use in the universal hash function
int prime1 = 16890581;
int c = rand()%10; // This is the random number c to use when finding g
int g = 0; // initial value of g
int x=0; // This is the sum of all the ascii values
// Create a for loop, to iterate from 1 to 1000 (1000 because that this how many cities are in the input file)
for(int i=1; i<=1000; i++){
for( int t = 1; i < city.length(); t++ ) // for loop that runs through the string
{
char ch=city.at(i);
int x=x+(int)ch; // This should be the running summation of the ascii values of the chars of the strings
}
}
g = (x*c)%prime1;
std::cout<<g;
return g; // returns the value of
}
int main(){
std:: string test = "hi";
std::cout<<CreateG(test);
}
这是我的错误
/home/ec2-user/environment/.c9/Project4/Hash.cpp:53:31: error:
‘CreateG’ was not declared in this scope
std::cout<<CreateG(test);
^
解决方案
CreateG
是hash
类的方法,不是独立的函数。
您必须实例化 anhash
才能使用它。
int main(){
std:: string test = "hi";
hash h;
std::cout<<h.CreateG(test);
}
推荐阅读
- javascript - 使用新参数在 div 中重新加载加载的 html
- javascript - 如何在 JavaScript 中复制 GZipStream.Write()?
- sql - Teradata - 按表的次数了解负载量
- swift - iOS 13 奇怪的搜索控制器间隙
- c# - 我想从 SQLite DB 中获取所有信息
- android - Android Studio 文档对话框耗时过长
- sql - Redshift - 过滤掉字符左侧或右侧空白的行
- c# - 在 View 中获取空引用异常。剃刀 MVC C#
- laravel - Laravel 查询生成器快速 JSON 连接不为空
- android - 从数据库中检索图像列表并使用 ListView 显示它们?