C++ C++;编译期间未找到函数
家庭作业:我应该创建随机字母键,将它们打印到文件中,然后使用下面代码中的函数“goodHash”将它们散列到哈希表中 当我试着运行下面的代码时,它说我的“goodHash”“标识符找不到”。我的代码怎么了C++ C++;编译期间未找到函数,c++,hash,C++,Hash,家庭作业:我应该创建随机字母键,将它们打印到文件中,然后使用下面代码中的函数“goodHash”将它们散列到哈希表中 当我试着运行下面的代码时,它说我的“goodHash”“标识符找不到”。我的代码怎么了 #include <iostream> #include <vector> #include <cstdlib> #include "math.h" #include <fstream> #include <time.h> using
#include <iostream>
#include <vector>
#include <cstdlib>
#include "math.h"
#include <fstream>
#include <time.h>
using namespace std;
// "makeKey" function to create an alphabetical key
// based on 8 randomized numbers 0 - 25.
string makeKey() {
int k;
string key = "";
for (k = 0; k < 8; k++) {
int keyNumber = (rand() % 25);
if (keyNumber == 0)
key.append("A");
if (keyNumber == 1)
key.append("B");
if (keyNumber == 2)
key.append("C");
if (keyNumber == 3)
key.append("D");
if (keyNumber == 4)
key.append("E");
if (keyNumber == 5)
key.append("F");
if (keyNumber == 6)
key.append("G");
if (keyNumber == 7)
key.append("H");
if (keyNumber == 8)
key.append("I");
if (keyNumber == 9)
key.append("J");
if (keyNumber == 10)
key.append("K");
if (keyNumber == 11)
key.append("L");
if (keyNumber == 12)
key.append("M");
if (keyNumber == 13)
key.append("N");
if (keyNumber == 14)
key.append("O");
if (keyNumber == 15)
key.append("P");
if (keyNumber == 16)
key.append("Q");
if (keyNumber == 17)
key.append("R");
if (keyNumber == 18)
key.append("S");
if (keyNumber == 19)
key.append("T");
if (keyNumber == 20)
key.append("U");
if (keyNumber == 21)
key.append("V");
if (keyNumber == 22)
key.append("W");
if (keyNumber == 23)
key.append("X");
if (keyNumber == 24)
key.append("Y");
if (keyNumber == 25)
key.append("Z");
}
return key;
}
// "makeFile" function to produce the desired text file.
// Note this only works as intended if you include the ".txt" extension,
// and that a file of the same name doesn't already exist.
void makeFile(string fileName, int n) {
ofstream ourFile;
ourFile.open(fileName);
int k; // For use in below loop to compare with n.
int l; // For use in the loop inside the below loop.
string keyToPassTogoodHash = "";
for (k = 1; k <= n; k++) {
for (l = 0; l < 8; l++) { // For-loop to write to the file ONE key
ourFile << makeKey()[l];
keyToPassTogoodHash += (makeKey()[l]);
}
ourFile << " " << k << "\n";// Writes two spaces and the data value
goodHash(keyToPassTogoodHash); // I think this has to do with the problem
makeKey(); // Call again to make a new key.
}
}
// Primary function to create our desired file!
void mainFunction(string fileName, int n) {
makeKey();
makeFile(fileName, n);
}
// Hash Table for Part 2
struct Node {
int key;
string value;
Node* next;
};
const int hashTableSize = 10;
Node* hashTable[hashTableSize];
// "goodHash" function for Part 2
void goodHash(string key) {
int x = 0;
int y;
int keyConvertedToNumber = 0;
// For-loop to produce a numeric value based on the alphabetic key,
// which is then hashed into hashTable using the hash function
// declared below the loop (hashFunction).
for (y = 0; y < 8; y++) {
if (key[y] == 'A' || 'B' || 'C')
x = 0;
if (key[y] == 'D' || 'E' || 'F')
x = 1;
if (key[y] == 'G' || 'H' || 'I')
x = 2;
if (key[y] == 'J' || 'K' || 'L')
x = 3;
if (key[y] == 'M' || 'N' || 'O')
x = 4;
if (key[y] == 'P' || 'Q' || 'R')
x = 5;
if (key[y] == 'S' || 'T')
x = 6;
if (key[y] == 'U' || 'V')
x = 7;
if (key[y] == 'W' || 'X')
x = 8;
if (key[y] == 'Y' || 'Z')
x = 9;
keyConvertedToNumber = x + keyConvertedToNumber;
}
int hashFunction = keyConvertedToNumber % hashTableSize;
Node *temp;
temp = new Node;
temp->value = key;
temp->next = hashTable[hashFunction];
hashTable[hashFunction] = temp;
}
// First two lines are for Part 1, to call the functions key to Part 1.
int main() {
srand ( time(NULL) ); // To make sure our randomization works.
mainFunction("sandwich.txt", 5); // To test program
cin.get();
return 0;
}
#包括
#包括
#包括
#包括“math.h”
#包括
#包括
使用名称空间std;
//“makeKey”函数创建字母键
//基于8个随机数字0-25。
字符串makeKey(){
int k;
字符串键=”;
对于(k=0;k<8;k++){
int keyNumber=(rand()%25);
if(keyNumber==0)
键。附加(“A”);
if(keyNumber==1)
键。附加(“B”);
if(keyNumber==2)
键。附加(“C”);
if(keyNumber==3)
键。附加(“D”);
if(keyNumber==4)
键。附加(“E”);
如果(键号==5)
键。附加(“F”);
if(keyNumber==6)
键。附加(“G”);
if(keyNumber==7)
键。附加(“H”);
if(keyNumber==8)
键。附加(“I”);
if(keyNumber==9)
键。附加(“J”);
如果(键号==10)
键。附加(“K”);
if(keyNumber==11)
键。附加(“L”);
if(keyNumber==12)
键。追加(“M”);
if(keyNumber==13)
键。附加(“N”);
如果(键号==14)
键。附加(“O”);
如果(键号==15)
键。附加(“P”);
if(keyNumber==16)
键。附加(“Q”);
if(keyNumber==17)
键。追加(“R”);
if(keyNumber==18)
键。附加(“S”);
如果(键号==19)
键。附加(“T”);
如果(键号==20)
键。附加(“U”);
如果(键号==21)
键。附加(“V”);
if(keyNumber==22)
键。附加(“W”);
if(keyNumber==23)
键。附加(“X”);
if(keyNumber==24)
键。附加(“Y”);
如果(键号==25)
键。附加(“Z”);
}
返回键;
}
//“makeFile”函数生成所需的文本文件。
//注意:只有在包含“.txt”扩展名时,此选项才起作用,
//而且同名的文件还不存在。
void makeFile(字符串文件名,int n){
流文件;
打开(文件名);
int k;//用于在下面的循环中与n进行比较。
int l;//用于下面循环中的循环。
字符串keyToPassTogoodHash=“”;
对于(k=1;k如果插入
void goodHash(string key);
在“使用命名空间…”下的行中,如果插入
void goodHash(string key);
在“使用命名空间…”下的行中问题是,如果您想在makeFile
中使用goodHash
,您必须在makeFile
之前向前声明goodHash
或定义goodHash
,否则,当编译在makeFile
中时,它会看到标记goodHash
,并且没有找到它的含义,这就是为什么会出现编译时错误
EDIT:问题是,如果您想在makeFile
中使用goodHash
,则必须在makeFile
之前向前声明goodHash
或定义goodHash
。否则,当编译在makeFile
中时,它会看到标记goodHash
,但没有发现这意味着什么,这就是为什么会出现编译时错误
编辑:C++希望所有内容都按顺序声明,以便在声明之前不使用任何内容。
如果需要引用文件中高于其定义位置的函数,则需要在声明该函数的文件顶部附近有一个函数原型。(因此,为所有函数编写原型是一种标准做法。)
在文件顶部附近(在#include
s之后),只需添加
void goodHash(string key);
定义
函数声明:声明函数名称和函数类型的东西
函数定义:指定函数的实际代码的东西。C++希望所有东西都按顺序声明,所以在声明之前没有使用任何东西。
如果需要引用文件中高于其定义位置的函数,则需要在声明该函数的文件顶部附近有一个函数原型。(因此,为所有函数编写原型是一种标准做法。)
在文件顶部附近(在#include
s之后),只需添加
void goodHash(string key);
定义
函数声明:声明函数名称和函数类型的东西
函数定义:指定函数实际代码的东西。您忘记了函数原型,只需在顶部添加以下内容:
void goodHash(string key);
顺便说一句,makeKey()太长了
您可以尝试以下方法:
string makeKey() {
int k;
string key = "";
for (k = 0; k < 8; k++) {
int keyNumber = (rand() % 25);
char app[2];
app[0] = keyNumber + 'A';
app[1] = 0;
key.append(app);
}
return key;
}
string makeKey(){
int k;
字符串键=”;
对于(k=0;k<8;k++){
int keyNumber=(rand()%25);
char-app[2];
应用程序[0]=密钥编号+A;
app[1]=0;
附加键(app);
}
返回键;
}
您忘记了功能原型,只需在顶部添加以下内容:
void goodHash(string key);
顺便说一句,makeKey()太长了
您可以尝试以下方法:
string makeKey() {
int k;
string key = "";
for (k = 0; k < 8; k++) {
int keyNumber = (rand() % 25);
char app[2];
app[0] = keyNumber + 'A';
app[1] = 0;
key.append(app);
}
return key;
}
string makeKey(){
int k;
字符串键=”;
对于(k=0;k<8;k++){
int keyNumber=(rand()%25);
char-app[2];
应用程序[0]=密钥编号+A;
app[1]=0;
附加键(app);
}
返回键;
}
谢谢!修复了问题。谢谢!修复了问题。谢谢!修复了问题。谢谢!修复了问题。我刚刚将整个goodHash函数移到顶部,它似乎正在工作。这应该没问题,对吧?或者如果我只是将