【发布时间】:2014-02-20 23:53:38
【问题描述】:
我在学习 C++ 时将 ifstream 参数传递给函数时遇到了问题。 不幸的是,仍在学习基础知识,所以我在网上找到解决方案或自己解决它都没有运气。
代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int TakefromFile(ifstream gradesinput);
void OutputtoFile();
char GradetoLetter();
void main()
{
int Studentcount = 0; // Variable that keeps track of the number of students calculated
ifstream gradesinput;
gradesinput.open("grades.txt");
ofstream gradesoutput;
gradesoutput.open("sorted_grades.txt");
while (gradesinput) // While the file has content to be read
{
TakefromFile(gradesinput); //Take data from the line
OutputtoFile(); //Add it to the output
Studentcount ++; //Increase studentcount by 1
}
gradesinput.close(); //Close both reading and writing files
gradesoutput.close();
}
int TakefromFile(ifstream gradesinput)
{
char firstname[20];
firstname[0] = gradesinput.get();
cout << firstname;
return 0;
}
void OutputtoFile()
{
}
char GradetoLetter()
{
}
错误出现在主函数“While Loop”中,特别是“TakefromFile”函数 因为“成绩输入”而打电话。调试时错误显示:
'std::basic_ifstream<_Elem,_Traits>::basic_ifstream' : cannot access private member declared in class 'std::basic_ifstream<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\fstream(827) : see declaration of 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
感谢任何帮助。如果这是我应该看到的简单的事情,我深表歉意。 谢谢!
【问题讨论】:
标签: c++ arguments private member ifstream