0

I am a python programmer looking for assistance with C++. I am trying to import a dictionary from a CSV file but I am getting the following error:

undefined reference to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream()'
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

map<string, string> hash_table;

void load_map(){

    // File pointer
    ifstream fin;
    fin.open("output.txt");

    // Read the Data from the file
    // as String Vector
    string temp;
    string key, delim, value;

    while (fin >> temp) {
        fin >> key;
        fin >> delim;
        fin >> value;
        hash_table[key] = value;
        }
}

int main()
{
    load_map();
    cout << hash_table["A12B12C11D11E11F11G10"];
    return 0;
}
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • 1
    Removing the superfluous `#include ` your code compiles without warning. What compiler are you using? See also: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) Compiler command used was `g++ -Wall -Wextra -pedantic -Wshadow -std=gnu++11 -Ofast -o readdictcsv readdictcsv.cpp` using `g++ (GCC) 10.1.0` – David C. Rankin Jun 18 '20 at 03:22
  • I am using the 'gcc' compiler. –  Jun 18 '20 at 03:31
  • 2
    You use `g++` for C++ and `gcc` for C -- is that your problem `:)` Look closely at the command line I used. The compiler is gcc, but you invoke `g++` to compile C++. – David C. Rankin Jun 18 '20 at 03:35
  • `std::map` is not a hash table. If you want a hash table use `std::unordered_map`. – eesiraed Jun 18 '20 at 03:39
  • @DavidC.Rankin, the file is now compiling, thank you. Although, I still think I am not reading in the file correctly, as the value from the hash_table is not been printed. –  Jun 18 '20 at 03:42
  • That's when it's time to put the debugging hat on. If you have a `key` of `"A12B12C11D11E11F11G10"` then you should get the corresponding value. Start with validating the read of `key, delim` & `value` (you should be validating every read, e.g. `if (!(fin >> key )) { // handle error }` and so on. You can simply `std::cout` each value from the first 10 lines or so of your input file. – David C. Rankin Jun 18 '20 at 03:47

0 Answers0