gpt4 book ai didi

c++ - 测试完美哈希函数时出现超出范围错误

转载 作者:行者123 更新时间:2023-11-30 04:48:20 25 4
gpt4 key购买 nike

我正在尝试测试我的散列项目的功能之一。此函数的目标是为包含 1000 个字符串的列表中的字符串创建索引。它应该将字符串中每个字符的 ASCII 值相加,将它乘以一个随机数 (int c),然后将它与一个素数 (int prime1) 取模,以在散列中找到索引。我试图用字符串“hi”测试这段代码,但每当我这样做时,我总是收到一个函数超出范围的错误。我不明白为什么 main 函数就在它试图测试的函数之下。

这是我的类(class)

 //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);
^

最佳答案

CreateGhash类的一个方法,不是一个独立的函数。

您必须实例化一个hash 才能使用它。

int main(){
std:: string test = "hi";
hash h;
std::cout<<h.CreateG(test);
}

关于c++ - 测试完美哈希函数时出现超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55885798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com