对 "class" 的引用不明确

Reference to "class" is ambigous

我想实现一个散列 table 示例。 因此,为了这个目的,我创建了一个 header、一个 hash.cpp 和 main.cpp 文件。 在我的 hash.cpp 中,我尝试 运行 一个虚拟散列函数,该函数采用键值并将其转换为索引值。但是,每当我尝试根据该散列 class.

创建 object 时,它会抛出错误(对 'hash' 的引用不明确)

这是我的 main.cpp:

#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>

using namespace std;

int main(int argc, const char * argv[]) {

    hash hash_object;
    int index;
    index=hash_object.hash("patrickkluivert");

    cout<<"index="<<index<<endl;
return 0;
}

这是我的 hash.cpp:

#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>


using namespace std;

int hash(string key){
    int hash=0;
 int index;
    index=key.length();

    return index;
}

这是我的hash.h

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
    public:
     int Hash(string key);

};

#endif /* defined(__hashtable__hash__) */

您的 hash class 与 std::hash 冲突。立即停止使用 using namespace std;。如果你想让打印语句更短,试试 using std::cout; using std::endl;

您的 hash class 符号与 std::hash

冲突

快速修复可能是使用全局命名空间限定符

int main(int argc, const char * argv[]) {

  ::hash hash_object;

但更好的并推荐的方法是停止使用

污染您的全局名称空间
using namespace std;

并在需要时使用 std::coutstd::endl。 如果您正在编写库,您还可以创建自己的命名空间。

此外,您这里有一些大写字母错别字:

index = hash_object.hash("patrickkluivert");
                    ^ I suppose you're referring to the Hash() function here

这里

int Hash(std::string key) {
    ^ this needs to be capital as well
  int hash = 0;

如果您想匹配您的声明并避免 cast/linking 错误。