我正在使用Pico.这是我得到的错误: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 hereTester.cxx:48: error: initializing argument 2 of 'StudRpt StoreAnswerKey(StudRpt, std::ifstream)'解决方案由于流不可复制,因此无法按值传递流.相反,传递对流(以及您的StudRpt)的引用:StudRpt StoreAnswerKey(StudRpt& Data, ifstream& inFile);StudRpt StoreStudData(StudRpt& Data, ifstream& inFile);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 constantconst int NUM_QTS = 15;// declare struct to hold info on student and answersstruct StudRpt { char answers[NUM_QTS]; string firstName; string lastName; char answerKey[NUM_QTS]; string testKey; string testData;};StudRpt Data;// function prototypesStudRpt 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 hereTester.cxx:48: error: initializing argument 2 of 'StudRpt StoreAnswerKey(StudRpt, std::ifstream)' 解决方案 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); 这篇关于通过函数传递ifstream文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 03:13