c++ - Reference to "class" is ambigous -


i implement hash table example. aim, have created 1 header, 1 hash.cpp , main.cpp files. in hash.cpp , tried run dummy hash function takes key value , turns index value. however, throws error(reference 'hash' ambiguous) whenever try create object according hash class.

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

this 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 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__) */ 

your hash class symbol clashing std::hash

a quick fix using global namespace qualifier

int main(int argc, const char * argv[]) {    ::hash hash_object; 

but better and recommended 1 stop polluting global namespace with

using namespace std; 

and using std::cout or std::endl when need them. create own namespace in case you're writing library.

besides, have capital letter typos here:

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

and here

int hash(std::string key) {     ^ needs capital   int hash = 0; 

in case want match declaration , avoid cast/linking errors.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -