C ++编译错误

时间:2016-04-13 04:33:32

标签: c++ compiler-errors fstream ifstream

#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;
}

错误如下:

fileAdder.cpp:在函数'int main()'中: fileAdder.cpp:36:40:错误:没有匹配函数来调用'std :: basic_ifstream :: close(const char *)'          inputFile.close(example.c_str());                                         ^ fileAdder.cpp:36:40:注意:候选人是: 在fileAdder.cpp:8:0中包含的文件中: /usr/include/c++/4.8.2/fstream:576:7:注意:void std :: basic_ifstream&lt; _CharT,_Traits&gt; :: close()[with _CharT = char; _Traits = std :: char_traits]        关()        ^ /usr/include/c++/4.8.2/fstream:576:7:注意:候选人需要0个参数,1个提供

2 个答案:

答案 0 :(得分:0)

ifstream::close确实not接受任何争论。

将第36行更改为inputFile.close();

答案 1 :(得分:-1)

删除example.c.str()

正确的陈述是:

inputFile.close();

我们不需要传递参数