微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决我的函数调用中不允许使用不完整的类型

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。