C++ 在cygwin中使用g++编译器编译时出现未定义的引用错误

C++ 在cygwin中使用g++编译器编译时出现未定义的引用错误,c++,reference,linker,undefined,undefined-reference,C++,Reference,Linker,Undefined,Undefined Reference,我刚开始为班级做一个项目,我遇到了一个错误,我不知道如何修复。我会尽我所能提供所有必要的细节,但如果你需要任何更多的信息,请让我知道。该项目是创建一个简单的哈希函数,我在尝试编译时遇到一个链接器错误。我将发布所有代码以及编译时收到的错误。我以前上过两次编程课,但那是很久以前的事了,我现在还没有练习,所以这可能是一个明显的错误。忽略hash.h中所有注释掉的代码,因为这只是我的老师提供的函数定义,我还没有实现。她还特别要求我们将散列函数放在它自己的单独文件中。我还应该指定我正在使用带有cygwin

我刚开始为班级做一个项目,我遇到了一个错误,我不知道如何修复。我会尽我所能提供所有必要的细节,但如果你需要任何更多的信息,请让我知道。该项目是创建一个简单的哈希函数,我在尝试编译时遇到一个链接器错误。我将发布所有代码以及编译时收到的错误。我以前上过两次编程课,但那是很久以前的事了,我现在还没有练习,所以这可能是一个明显的错误。忽略hash.h中所有注释掉的代码,因为这只是我的老师提供的函数定义,我还没有实现。她还特别要求我们将散列函数放在它自己的单独文件中。我还应该指定我正在使用带有cygwin64的Windows机器来编译代码

h.h:

#ifndef __HASH_H
#define __HASH_H

#include <string>
#include <list>

using std::string;
using std::list;

class Hash {

public:
  void remove(string);              // remove key from hash table
  //void print();                     // print the entire hash table
  void processFile(string);         // open file and add keys to hash table
  //bool search(string);              // search for a key in the hash table
  //void output(string);              // print entire hash table to a file
  //void printStats();                // print statistics

private:
   // HASH_TABLE_SIZE should be defined using the -D option for g++
   //list<string> hashTable [HASH_TABLE_SIZE];
   list<string> hashTable [100];
   int collisions;
   int longestList;
   double avgLength;

   int hf(string);                  // the hash function

   void insert(string);

// put additional variables/functions below
// do not change anything above!

};

#endif

define _HASH_H触发未定义的行为,因为以_开头的标识符是保留的。return[sum%100/*HASH_TABLE_SIZE*/];这不可能编译。@rightfold:我试图更改它来定义HASH_H,但没有任何区别,谢谢你的回答。@t.C:这到底是为什么?只需返回[sum%100],并在100后面加上注释。如果语法不正确,我不会像您所期望的那样,在这一行代码上出现任何错误。我试着改变那一行,但没什么不同,谢谢你的帮助。[sum%100]根本不是一个有效的表达。
#include "hash.h"

void Hash::remove(string string_to_remove)
{
    int index = hf(string_to_remove);

    for(list<string>::iterator iter = hashTable[index].begin(); iter != hashTable[index].end();      iter++)
    {
        if(*iter == string_to_remove)
            hashTable[index].erase(iter);
    }

}

void Hash::insert(string string_to_add)
{
    int index = hf(string_to_add);

    hashTable[index].push_back(string_to_add);
}

void Hash::processFile(string file_name)
{
    insert(file_name);
}
#include "hash.h"

int Hash::hf(string input)
{
    int sum = 0;

    for (int i = 0; i < 5; i++)
         sum += (int)input[i];

    cout << sum % 100 << endl;

    return [sum % 100 /*HASH_TABLE_SIZE*/];
}
$ g++ hash_function.cpp testmain.cpp hash.cpp -o test
/tmp/cc42z5XM.o:hash.cpp:(.text+0x32): undefined reference to `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x32): relocation truncated to fit: R_X86_64_PC32 against   undefined symbol `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x13c): undefined reference to `Hash::hf(std::string)'
/tmp/cc42z5XM.o:hash.cpp:(.text+0x13c): relocation truncated to fit: R_X86_64_PC32 against     undefined symbol `Hash::hf(std::string)'
collect2: error: ld returned 1 exit status