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

时间:2009-10-17 17:18:43

标签: 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;
}

2 个答案:

答案 0 :(得分:6)

是的,问题就在于行

void read_data(finput, foutput);

主要功能内部。 调用函数时,请勿指定返回类型。只有在宣布它时。换句话说,该行应该只读

read_data(finput, foutput);

答案 1 :(得分:1)

在你的调用之前你有空,这是函数的签名 - 你实际上并没有调用它,这就是编译器抱怨的原因。