0
votes

I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string) Anyone know how to solve this problem?

hash.h

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;

#ifndef HASH_H
#define HASH_H

class hash{
public:

    int Hash(string key);
};

#endif // HASH_H

hash.cpp

#include <iostream>
#include <cstdlib>
#include <string>

#include "hash.h"

using namespace std;


int hash::Hash(string key){

    //int hash = 0;
    int index;
    index = key.length();

    return index;
}

main.cpp

#include <iostream>
#include <cstdlib>
#include <string>

#include "hash.h"

using namespace std;

int main()
{
    int index;
    hash hashOb;
    string traget = "Testing";
    index = hashOb.Hash(traget);

    cout << index << endl;

    return 0;
}

Im using CodeBlock 13.12 There is only main.o file in obj folder. I dont know why the hash.o isn't there.

1
I'd suggest you start to use different names instead of playing with capitals, the code is pretty hard to read like this. Also there is no need to repeat all those includes all over the place. Just include what you need. Also the ifndef guard has to be for the entire file (including the #includes), not just the class. And 'using namespace std' is bad practice and might as well be the cause here since std::hash exists (though afaik it's not in those headers). You should probably read some introductionary C++ books. - stijn
It sounds that you need to add the hash.cpp file explicitly to your code blocks project. - πάντα ῥεῖ
Let's say I change the int Hash(string key) function to int GetIndex(string key) to avoid overriding. - FisNaN
After I add hash.cpp to my project again. It works now. Thank you - FisNaN

1 Answers

1
votes

hash is an inbuilt template in "std" namespace.

Try removing using namespace std; lien and use namespace name as and when required.