Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No matching constructor for initialisation of 'ifstream'

I am geting an an error in the following code it worked fine in visual studio but once i have moved it over to Xcode that uses gcc to compile get this error No matching constructor for initialisation of 'ifstream' i have looked at adding this as a reference rather than a copy as suggested on this site but it still came up with the error.

void getAndSetTextData::GetBannedList(string fileName)
{
    bannedWordCount = 0;
    ifstream inFile(fileName);
    while(inFile >> currentWord)
    {
        bannedWords.push_back(currentWord);
        bannedWords[bannedWordCount++] = currentWord;
    }
    inFile.close();
}  

Any help would be appreciated.

like image 636
bobthemac Avatar asked Mar 07 '12 20:03

bobthemac


2 Answers

ifstream constructor accepts a const char* as the filename (prior C++11):

ifstream inFile(fileName.c_str());

An additional constructor that accepts a const std::string& as the filename was added in C++11.

Minor point: consider changing argument string fileName to const string& fileName to avoid unnecessary copy of fileName.

like image 90
hmjd Avatar answered Nov 18 '22 20:11

hmjd


first you should check that weather the file is opened or not. for example if you dont have permission to access the file or if you are opening a file in write mode when there is no enough disk space, etc... so

ifstream inFile(fileName);
if( ! inFile )
   return;
while(inFile >> currentWord)

and about your question, are you including the fstream?

like image 1
rene Avatar answered Nov 18 '22 20:11

rene