rosie
rosie

Reputation: 1

No matching function for call when reading file

I'm getting an error when I'm trying to open my file and perform a read operation on my second file. I'm not sure what went wrong.

error: no matching function for call to ‘std::basic_fstream<char>::open(std::string&)’
     file.open(filename);
int main()
{
    DoublyLinkedBag<string> dictionary;
    fstream file;
    string word;
    file.open("dictionary.txt", ios::in); // open a file to perform read operation using file object
    if (file.is_open()) // check whether file is open
    {
        while (file >> word)
        {
            dictionary.add(word);
        }
    }

    string filename;
    string words;
    cout << "Enter the name of the file that contains words to check:" << endl;
    cin >> filename;
    file.open(filename);
    if (file.is_open())
    {
        while (file >> words)
        {
            if (!dictionary.contains(words))
            {
                cout << "The following words in the file " << filename << " are not spelled correctly:" << endl;
                cout << words << endl;
                cout << "Thanks for using the spell checker system." << endl; 
            }
        }
    }
    file.close();
}

Upvotes: 0

Views: 1269

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595971

The error message is self-explanatory. open() in the version of C++ you are compiling for does not accept a std::string as a parameter. That overload was added in C++11.

So, either update your project to compile for C++11 or later, or else for older versions you will have to use this instead:

file.open(filename.c_str());

Upvotes: 1

Related Questions