#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
string example;
int numbers,
totalNumbers = 0;
// Prompt user to input name of the file to open
cout << "Please enter the name of the program" << endl;
cin >> example;
// Open the input file that is assigned to the variable 'example'
inputFile.open(example.c_str());
// If the file successfully opened, process it
if(inputFile)
{
// Loop until the EOF is reached
while(inputFile >> numbers) // If a value was read
{
totalNumbers += numbers;
}
// Close the file
inputFile.close(example.c_str());
cout << totalNumbers;
}
else
{
// Display an error message
cout << "could not access file";
}
return 0;
}
Der Fehler wird wie folgt:C++ kompiliert Fehler
fileAdder.cpp: In Funktion 'int main()': fileAdder.cpp: 36: 40: Fehler: keine Anpassungsfunktion für Aufruf von ' std :: basic_ifstream :: schließen (const char *) ' inputFile.close (beispiel.c_str()); ^ fileAdder.cpp: 36: 40: Hinweis: Kandidat ist: In Datei enthalten von fileAdder.cpp: 8: 0: /usr/include/c++/4.8.2/fstream:576:7: Hinweis: void std :: basic_ifstream < _CharT, _Traits> :: close() [mit _CharT = char; _Traits = std :: char_traits] close() ^ /usr/include/c++/4.8.2/fstream:576:7: Anmerkung: Kandidaten 0 Argumente erwartet, 1 bereitgestellt
lesen passieren Bei der Fehlermeldung wird das Problem ganz deutlich angegeben. – anukul