我的函数调用中不允许使用不完整的类型

问题描述

在尝试调用函数时,我的代码出错。我已经尝试了几种不同的方法,但我无法让它工作,我知道它一定是愚蠢的。

#include <iostream>
#include <fstream>
using namespace std;

void openFile(string input);

int main()
{
    string input;
    cout << "Please enter a file to open: " << endl;
    cin >> input;

    void openFile(input);  //error is right here!!!!!!!!!

    return 0;
}



void openFile(string input) {

    ifstream in_file;

    in_file.open(input);
    if (in_file.fail())
        cout <<  "Something went wrong,file did not open!!!" << endl;
    else {
        cout << "File opened successfully!!!" << endl;
        cout << in_file.rdbuf() << endl;
    }
    in_file.close();
}

解决方法

您不应该为调用函数编写 void,它是用于函数原型或减速的返回类型。下面是正确的代码。

int main()
{
    string input;
    cout << "Please enter a file to open: " << endl;
    cin >> input;

    openFile(input); // function calls must not be preceded with a void keyword 

    return 0;
}