读取文件时没有用于调用的匹配函数



当我试图打开我的文件并对第二个文件执行读取操作时,我遇到了一个错误。我不确定出了什么问题。

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();
}

错误消息不言自明。您正在编译的C++版本中的open()不接受std::string作为参数。这个过载是在C++11中添加的。

因此,要么更新您的项目以编译C++11或更高版本,要么对于旧版本,您将不得不使用以下内容:

file.open(filename.c_str());

最新更新