bob*_*mac 5 c++ class file ifstream
我在下面的代码中发现了一个错误,它在visual studio中运行良好,但是一旦我将它移动到使用gcc编译的Xcode得到这个错误没有匹配的构造函数用于初始化'ifstream'我已经看过将此添加为引用而不是本网站上建议的副本,但它仍然提出错误.
void getAndSetTextData::GetBannedList(string fileName)
{
bannedWordCount = 0;
ifstream inFile(fileName);
while(inFile >> currentWord)
{
bannedWords.push_back(currentWord);
bannedWords[bannedWordCount++] = currentWord;
}
inFile.close();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
hmj*_*mjd 11
ifstream构造函数接受a const char*作为文件名(先前的C++ 11):
ifstream inFile(fileName.c_str());
Run Code Online (Sandbox Code Playgroud)
const std::string&在C++ 11中添加了一个接受a 作为文件名的附加构造函数.
次要的一点:考虑改变参数string fileName,以const string& fileName避免不必要的副本fileName.