编译期间找不到C ++函数

时间:2012-06-10 22:05:18

标签: c++ hash

对于家庭作业:我应该创建随机的字母键,将它们打印到文件中,然后使用函数“goodHash”将它们中的每一个哈希到哈希表中,可以在下面的代码中找到。

当我尝试运行以下代码时,它表示我的“goodHash”“标识符未找到”。我的代码出了什么问题?

#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;
}

我发现我的代码在某些部分很麻烦,但我在C ++中是一个菜鸟并且不太了解它做得更好。

我猜我可以做的另一种方法是在将字母键写入文件后,从文件中读取它们并在我这样做时散列每个键,但我不知道如何编写

4 个答案:

答案 0 :(得分:3)

如果你插入

void goodHash(string key);

在“使用命名空间...”下的行中,它将起作用

答案 1 :(得分:3)

C ++期望按顺序声明所有内容,以便在声明之前不会使用任何内容。 如果您需要引用文件中比函数更高的函数,则需要在文件顶部附近有一个声明函数的函数原型。 (编写所有函数的原型是一种标准做法。)

靠近文件顶部(#include之后),只需添加

即可
void goodHash(string key);

解释

函数声明:声明函数名称和函数类型的东西。

函数定义:指定函数实际代码的东西。

答案 2 :(得分:2)

问题是,如果您想在goodHash中使用goodHash,则必须在makeFile之前转发声明goodHash或定义makeFile。否则,当编译在makeFile时,它会看到令牌goodHash并且没有找到它的含义,这就是你得到编译时错误的原因。

编辑Here is a good resource on forward declarations

答案 3 :(得分:0)

你忘记了函数原型,只需将其添加到顶部:

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;
}