c ++中的引用含糊不清

时间:2016-10-31 01:52:13

标签: c++ hash

我对C ++很陌生,我试图从youtube重现哈希表项目。当我用新的头文件" hash.h",一个main.cpp和一个hash.cpp创建一个新项目时,当我编译并运行main.cpp时,我得到一个错误说我的"哈希"是暧昧的。我的想法是我的哈希与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;

}

hash.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

hash.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;
}

2 个答案:

答案 0 :(得分:0)

您确实与标准库存在名称冲突,正如您所怀疑的那样。

直接修复:

•在主程序中,将声明更改为

::hash hashObj;             //!

•在hash.cpp文件中,添加std::资格:

int hash::Hash(std::string key){        //!

而不是上面显示的直接修复,我将为自定义哈希表的内容引入一个命名空间。然后你可以写例如my::hash。另外,我在类型名称中使用我常用的第一个字符大写的命名约定,比如Hash,然后问题首先不在那里(但我仍然使用命名空间)。

答案 1 :(得分:0)

始终不想使用#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; } 。 或者编译器将无法确定您引用的哈希类。

这将编译:

"drawCallback": function ( settings ) {
   drawCallback(this.api());                
}