c++ - 需要帮助解决三个 visual studio 错误 - 尝试构建解决方案时出现 C++ 错误

标签 c++ visual-studio

当我尝试构建这个项目时出现以下错误:

error C2182: 'read_data':illegal use of type 'void'
error C2078: too many initializers
errors c2440: 'initializing': cannot convert from 'std::ofstream' to int

以上所有内容似乎都指向我在第 72 行的函数调用,即这一行: void read_data(finput, foutput);

我在 MSDN 站点上查找了这些错误代码,但无法使用描述来推断可能出现的问题。

欢迎任何想法/提示。

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

void read_data(ifstream& finput, ofstream& foutput);
//PRE: The address of the ifstream & ofstream objects is passed to the function
//POST: The data values are read in until the end of the file is reached

void print_data(ofstream& foutput, string fname, string lname, int largest, int smallest);
//PRE: The address of the ofstream object and the values of fname, lname and largest and smallest integer
//     in each row is passed to the function
//POST: The values are outpur to the file with formatting

int max(int num1, int num2, int num3, int num4);
//PRE: Four integer values are passed to the function
//POST: The largest of the four integer values is returned

int min(int num1, int num2, int num3, int num4);
//PRE: Four integer values are passed to the function
//POST: The smallest of the four integer values is returned

int main()
{
    //Declare the filestream objects
    ifstream finput;
    ofstream foutput;

    //Attempt to open the input & output files
    //In each case, print an error message and quit if they fail to open
    finput.open("program4_input.txt");
    if (finput.fail())
    {
        cout <<"The input file failed to open!" << endl;
        return exit(1);
    }

    foutput.open("output.txt");
    if (foutput.fail())
    {
        cout <<"The output file failed to open!" << endl;
        return exit(2);
    }

    void read_data(finput, foutput);

    return 0;
}


//Function definitions
void read_data(ifstream& finput, ofstream& foutput)
{
    string fname, lname;
    int num1, num2, num3, num4, largest, smallest;

    while(finput >> fname)
    {
        finput  >> lname >> num1 >> num2 >> num3 >> num4;
        largest = max(num1, num2, num3, num4);
        smallest = min(num1, num2, num3, num4);

        print_data(foutput, fname, lname, largest, smallest);


    }

}

void print_data(ofstream& foutput, string fname, string lname, int largest, int smallest)
{
    foutput << setw(15) << fname << setw(15) << lname << setw(10) << largest << setw(10) << smallest 
            << endl;
}

int max(int num1, int num2, int num3, int num4)
{
    int lnum, lnum1, lnum2;

    if (num1 > num2)
    {
        lnum1 = num1;
    }
    else
        lnum1 = num2;


    if (num3 > num4)
    {
        lnum2 = num3;
    }
    else
        lnum2 = num4;


    if (lnum1 > lnum2)
    {
        lnum = lnum1;
    }
    else
        lnum = lnum2;

    return lnum;
}


int min(int num1, int num2, int num3, int num4)
{
    int snum, snum1, snum2;

    if (num1 < num2)
    {
        snum1 = num1;
    }
    else
        snum1 = num2;


    if (num3 > num4)
    {
        snum2 = num3;
    }
    else
        snum2 = num4;


    if (snum1 > snum2)
    {
        snum = snum1;
    }
    else
        snum = snum2;

    return snum;
}

最佳答案

是的,是线路的问题

void read_data(finput, foutput);

在主函数中。 调用 函数时不要指定返回类型。仅在声明时。换句话说,该行应该只是阅读

read_data(finput, foutput);

关于c++ - 需要帮助解决三个 visual studio 错误 - 尝试构建解决方案时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1582704/

相关文章:

字符串流istringstream的C++问题

visual-studio - 使用 Visual Studio 调试器在值更改时中断

.net - 在字符串中包含引号?

visual-studio - 使用参数启动外部程序(在 Visual Studio - 调试属性中)

c++ - std::map\std::set 包含重复键

c++ - matlab在opencv中查找函数实现?

c++ - 显式复制构造函数行为和实际用途

c++ - C++ 应用程序的框架

c# - Visual Studio 2015 添加服务引用

c++ - 在 Windows 中使用事件进行调试