2009-10-17 1 views
1

을 구축하려고 할 때 C++ 오류가 발생하는 :세 비주얼 스튜디오 오류 도움이 필요 -이 프로젝트를 빌드 할 때 솔루션에게 나는 다음과 같은 오류를 얻을

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

답변

6

예, 문제는 메인 함수 내부의 라인

void read_data(finput, foutput); 

입니다. 함수를 호출 할 때 반환 유형을 지정하지 마십시오. 선언 할 때만. 즉, 라인은

read_data(finput, foutput); 
+0

arrghhh i i를 참조하십시오. 나는 내가 30 분 동안 그것에 응시했다라고 생각한다. 그리고 didnt 이벤트는 그것을 본다. 소년은 어리 석다. 고맙습니다! – noobzilla

+0

obijuan, 이것이 해결책 인 경우 대답을 수락해야합니다. – sbi

1

당신은 함수의 서명입니다 전화하기 전에 무효가 읽어야합니다 - 당신이 실제로 컴파일러가 불평 이유입니다 그것을 호출되지 않습니다.

+0

고마워요, 매기. 소년, 나는 바보 같습니까? – noobzilla

+1

멍청한 느낌이 들지 마라. 실수를 놓치기 쉽다. 그러면 바퀴를 돌리는 것이 더 좋다. :) – Maggie

관련 문제