c++ - C++ 中的引用不明确

标签 c++ hash

我对 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/

相关文章:

戈朗 : sha256 returns two different values for the same input

c++ - 如何使用std::find查找具有struct成员值的set元素?

SVM 的 C++ 数据

c++ - boost::asio 同步服务器在第一个之后不接受连接

c - 将 n 个值分配给大小为 n 的数组的时间复杂度是 O(n) 吗?

android - 如何判断 Play Integrity 判决中的证书 SHA-256 摘要是否有效?

python - 字典和哈希表之间的真正区别是什么?

c++ - 将 vector 放入 map

c++ - 常量存储 C++?

php - mcrypt 已弃用? - 如何在 PHP 中正确加密和保存密码?