jeudi 9 juillet 2020

Constructor returns compilation error when passed with a reference to ifstream [duplicate]

EDIT: I forgot to include the Hash.cpp in my MakeFile, problem has been solved...

I am trying to create a Hash class that reads input from a file and then outputs it to the screen. The class is required (I am planning to upgrade the hash class for more functionality later), but the compiler is giving me a strange error which I cannot debug.

My hash header is as follows:

#ifndef HASH_H
#define HASH_H
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;

class Hash{
    public:
        Hash(ifstream& file, string hashFunctionName);
        int strLengthHash(ifstream& file);
    private:
        const int SIZE = (int)pow(2,16);
        vector<int>hashTable;
};
#endif

Now, here's hash.cpp:

#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <string>
#include "Hash.h"
using namespace std;

Hash::Hash(ifstream& file, string hashFunctionName){
    hashTable = vector<int>(SIZE);
    if(hashFunctionName == "strLengthHash"){
        strLengthHash(file);
    }
}

int Hash::strLengthHash(ifstream& file){
    string line;
    
    if (file.is_open())
    {
        while(getline(file,line) )
        {
            cout << line << endl;
        }
        file.close();
    }
    return 0;
}

And my Tester file is the following:

#include <iostream>
#include <vector>
#include <fstream>
#include "Hash.h"
using namespace std;

int main(){
    ifstream myfile("keys.txt");
    Hash strHash(myfile, "strLengthHash");

    return 0;
}

The compiler gives me the following error:

Undefined symbols for architecture x86_64:
  "Hash::Hash(std::__1::basic_ifstream<char, std::__1::char_traits<char> >&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
      _main in Tester.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test] Error 1

...which I cannot understand at all. Can somebody tell me what this error is saying and how to fix it?

Aucun commentaire:

Enregistrer un commentaire