2

This program doesn't compile in Visual C++, but from some reason it worked with barely any problems when running it with g++ -std=c++11 and an online compiler(of course,when I comment the #include "stdafx.h").

#include "stdafx.h"
#include <istream>
#include <fstream>
#include <unordered_map>
#include <map>
#include <iostream>
#include <vector>

using namespace std;

class Hashmap
{
std::multimap<char, string> htmap;

public:
void put(char key, const string value)
{
    pair<char, string> m(key, value);
    this->htmap.insert(pair<char, string>(key, value));
}


int isIn(char c, vector<char> v)
{
    for (auto s : v)
        if (c == s)
            return 0;
    return 1;
}

const void get(char key, string word)
{
    int j = word.length();
    vector<char> v;
    vector<char> v2;
    multimap<char, string>::const_iterator Values = this->htmap.find(key);
    int Number = this->htmap.count(key);
    for (int q = 0; q < Number; q++)
    {
        int j = word.length();
        int ok = 1;
        string a = Values->second;
        for (int k = 0; k< word.length(); k++)
            v2.push_back(word[k]);
        for (int i = 0; i< a.length(); ++i)
        {
            if (isIn(a[i], v2) == 1)
                ok = 0;
        }
        v.clear();
        v2.clear();
        if (ok == 1)
        {
            cout << "Word Found: "<< Values->second << endl; // Error Here
        }
        ++Values;
    }
}
void find(const string word)
{
    string s;
    int j = word.length();
    for (int k = 0; k<j; k++)
    {
        char c = word[k];
        this->get(c, word);
    }
}
};

void read(Hashmap t)
{
string key, value;
ifstream f("in.txt");
while (f >> key) // Error Here
{
    for (auto i : key)
        t.put(i, key);
}
string word;
cout << "Word: ";
std::cin >> word; // Error Here
t.find(word);
f.close();
}

int main()
{
    Hashmap t;
    string word;
    read(t);
    return 0;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Paul
  • 121
  • 1
  • 8

1 Answers1

2

but from some reason it worked with barely any problems when running it with g++ -std=c++11 and an online compiler ...

I'd suspect you've been missing to

#include <string>

while using std::string extensively in your code.

Always include standard c++ headers corresponding to the classes/functions you are using in your code.

Some compilers will just work with forward declarations of std::string in header files like iostream, others will actually include the string header.


Also note:

using namespace std;

isn't such a good idea in most cases. Better explicitly use the std:: scope for what you meant to call.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Zut. Forgot I'd added #include before removing the `using namespace std;`. Both changes were needed to build under VS 2015. – user4581301 May 18 '16 at 21:45