1
votes

I have a couple errors that I don't understand and am not even sure how to go about searching this on the internet. I've tried "Opening files in C++ function," as well as the exact error, 0 but didn't get much help in the first few pages. This has been giving me a headache for a while now, and not even my instructor could help a whole lot. Though this IS the way he wanted it done, somehow. And yes, this is for a homework assignment.

The errors:

\driver.cpp: In function void openFile(std::ifstream&, std::ofstream&)': \driver.cpp:63: error: no matching function for call tostd::basic_ifstream >::open(std::string&)' ../include/c++/3.4.2/fstream:570: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits] J:\Class_Files_2013_Fall\Advanced C++\Week1\Lab_2(Letter_Counter)\driver.cpp:68: error: no matching function for call to `std::basic_ofstream >::open(std::string&)' ../include/c++/3.4.2/fstream:695: note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]

So in main, I have:

    ifstream inFile;                    
//defines the file pointer for the text document
    ofstream outFile;                   
//the file pointer for the output file

    openFile(inFile, outFile);          
//allow the user to specify a file for reading and outputting the stats

And then the function:

void openFile(ifstream& inF, ofstream& outF){
    // Ask the user what file to READ from
    string readFile, writeFile;
    cout << "Enter the name of the file you want to READ from: ";
    cin >> readFile;
    cout << endl;
        // Open the File
    inF.open(readFile);
    // Ask the user what file to WRITE to
    cout << "Enter the name of the file you want to WRITE to: ";
    cin >> writeFile;
    cout << endl;
    outF.open(writeFile);
}

I have also implemented the:

#include <fstream>

And:

using namespace std;

The things that were in the main were put there by the instructor, so I can't change those. In other words, I HAVE to pass the file pointers into the openFile function and ask the user for the names of the files. However, I was never taught how to do this. A basic answer would be appreciated, going off what I've already been doing.

3

3 Answers

2
votes

Only C++11 supports opening fstreams with std::strings.

Either compile your app with C++11 compatibility (depends on the compiler, for clang and gcc it's -std=c++11), or change your calls to inF.open(readFile.c_str());

2
votes

Easy fix:

inF.open(readFile.c_str());
outF.open(writeFile.c_str();

Both functions expect you to pass in a const char* as the first argument. You are trying to pass it a string.

1
votes
no matching function for call to std::basic_ifstream::open(std::string&)

When you see a "no matching function" error it means the compiler searched for but could not find a function that takes the argument you gave. In this case, it cannot find an overload of the function open() that takes a std::string&.

As of C++11, the input and output file stream classes provide overloads of their constructors and the open() member function that take std::string as an argument. Unfortunately, if your compiler does not support C++11, you'll have to go with the overload that takes a const char* instead. You can grab a pointer to the buffer by calling the c_str() method:

inF.open(readFile.c_str());   // calls open(const char*)
outF.open(writeFile.c_str()); // calls open(const char*)