Errors when passing ifstream file through a functi

2019-03-06 05:06发布

Here is my code:

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

// include .h file that holds function ot write header
#include "WriteE3RptHdr.h"

// declare global constant
const int NUM_QTS = 15;

// declare struct to hold info on student and answers
struct StudRpt {
    char answers[NUM_QTS];
    string firstName;
    string lastName;
    char answerKey[NUM_QTS];
    string testKey;
    string testData;
};

StudRpt Data;

// function prototypes
StudRpt StoreAnswerKey(StudRpt Data, ifstream inFile);
StudRpt StoreStudData(StudRpt Data, ifstream inFile);
void WriteRpt(StudRpt Data, ofstream& outFile);

int main()
{
    int correct;
    int inforrect;
    int score;
    // delcare and open file streams
    ifstream inFile;
    ofstream outFile;
    inFile.open("in.data");
    outFile.open("out.data");

    // call function included from .h file
    WriteRptHdr(outFile);

    StoreAnswerKey(Data, inFile);

    // priming read
    StoreStudData(Data, inFile);

    while (inFile) {
        StoreStudData(Data, inFile);
        WriteRpt(Data, outFile);
    }
    inFile.close();
    outFile.close();
}
//----------------------------------------------------
StudRpt StoreAnswerKey(StudRpt Data, ifstream inFile) {
    int i;
    for (i = 0; i < NUM_QTS; i++) {
        inFile >> Data.answers[i];
    }

    return Data;
}

The function WriteRptHdr is included from the .h file. it passes an ofstream through a function-- I've tested it, and it works just fine. But the ifstream inFile doesn't work (the program is meant to read in a answer key, and compare them to the answers a student gives. I can't get it to read in the answer key from the ifstream being passed)

I'm using Pico. This is what I get as an error:

In function 'int main()':
Tester.cxx:48: note: synthesized method 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)' first required here
Tester.cxx:48: error: initializing argument 2 of 'StudRpt StoreAnswerKey(StudRpt, std::ifstream)'

1条回答
该账号已被封号
2楼-- · 2019-03-06 06:10

You cannot pass the stream by value, since streams are not copyable.

Instead, pass a reference to the stream (as well as your StudRpt):

StudRpt StoreAnswerKey(StudRpt& Data, ifstream& inFile);
StudRpt StoreStudData(StudRpt& Data, ifstream& inFile);
查看更多
登录 后发表回答