我对 C++ 非常陌生,我正在尝试从 youtube 重现哈希表项目。当我使用新的 header filr“hash.h”、main.cpp 和 hash.cpp 创建新项目时,当我编译并运行 main.cpp 时,我收到一条错误,指出我的“hash”是模棱两可的。我的想法是我的哈希与 std::hash 发生冲突,这就是错误的来源,但我不太确定如何纠正它..请帮助!这是在 Code::Blocks 中完成的:)
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main(){
int index;
hash hashObj;
index = hashObj.Hash("Amanda");
cout << index << endl;
return 0;
}
哈希.h
#include <iostream>
#include <cstdlib>
#include <string>
#ifndef HASH_H_INCLUDED
#define HASH_H_INCLUDED
class hash{
public:
int Hash(std::string key);
};
#endif // HASH_H_INCLUDED
哈希.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
int hash::Hash(string key){
int hash = 0;
int index;
index = key.length();
return index;
}
最佳答案
总是不喜欢使用using namespace std
。
否则编译器将无法确定您引用的是哪个哈希类。
这将编译:
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
int main(){
int index;
hash hashObj;
index = hashObj.Hash("Amanda");
std::cout << index << std::endl;
return 0;
}
关于c++ - C++ 中的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40335604/