对"class"的提及含糊不清

Reference to "class" is ambigous

本文关键字:含糊不清 class      更新时间:2023-10-16

我想实现一个哈希表的例子。为此,我创建了一个头文件、一个hash.cpp文件和一个main.cpp文件。在我的hash.cpp中,我尝试运行一个虚拟哈希函数,它接受键值并转换为索引值。然而,每当我尝试根据该哈希类创建对象时,它就会抛出一个错误(对'hash'的引用是模糊的)。

this is my main.

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

this is my 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;
}

this is my 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类符号与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;

如果你想匹配你的声明并避免强制转换/链接错误

您的hash类与std::hash冲突。马上停止使用using namespace std;。如果您想使打印语句更短,请尝试using std::cout; using std::endl;