【问题标题】:Incomplete type is not allowed in my function call我的函数调用中不允许不完整的类型
【发布时间】:2021-07-18 06:27:27
【问题描述】:

尝试调用函数时,我的代码中出现错误。我已经尝试了几种不同的方法,但我就是无法让它发挥作用,我知道这一定很愚蠢。

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

【问题讨论】:

  • 在给出路径时是否给出了双反斜杠,而不是单反斜杠?
  • 投票关闭为错字:这不是你调用函数的方式。
  • 哎呀,我没注意,是的,他的语法不正确
  • @Jeremy Mahan 你不应该写一个 void 来调用一个函数。

标签: c++ incomplete-type


【解决方案1】:

您不应该为调用函数编写 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;
}

【讨论】:

  • 谢谢,我总是想念一些愚蠢的事情......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多