C++ C+中的fstream错误+;
我正在编写一个简单的程序来输入学生记录并将其存储在一个分隔文件中。一切看起来都很好,但当我运行程序时,出现了一个错误: 错误1错误C2248:'std::basic_ios::basic_ios':无法访问在类'std::basic_ios'c:\program files\microsoft visual studio 10.0\vc\include\fstream 1116 1 project1中声明的私有成员 代码如下:C++ C+中的fstream错误+;,c++,C++,我正在编写一个简单的程序来输入学生记录并将其存储在一个分隔文件中。一切看起来都很好,但当我运行程序时,出现了一个错误: 错误1错误C2248:'std::basic_ios::basic_ios':无法访问在类'std::basic_ios'c:\program files\microsoft visual studio 10.0\vc\include\fstream 1116 1 project1中声明的私有成员 代码如下: #include <cstring> #include
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void closeOrNewRecordMenu(string enterAnotherRecord)
{
if (enterAnotherRecord == "Q" && enterAnotherRecord == "q")
{
exit(0);
}
}
void newStudentRecord(double studentNumber, string firstName, string lastName, string campus, string course1, string course2, string course3, string seniorPracticum, ofstream writeToRecordsFile)
{
int campusChoice;
do {
cout << "Student's six digit number: (Numeric only)";
cin >> studentNumber;
cin.ignore();
}
while (studentNumber < 100000 && studentNumber > 999999);
cout << "Student's first name: " << "\n";
cin >> firstName;
cin.ignore();
cout << "Student's last name: " << "\n";
cin >> lastName;
cin.ignore();
while (campusChoice < 1 || campusChoice > 3)
cout << "Which campus will " << firstName << " " << lastName << " be attending class at: " << "\n";
cout << "For the North campus enter 1" << "\n";
cout << "For the South campus enter 2" << "\n";
cout << "For the Seaside campus enter 3" << "\n";
cin >> campusChoice;
cin.ignore();
if (campusChoice == 1)
{
campus = "North Campus";
}
else if (campusChoice == 2)
{
campus = "South Campus";
}
else if (campusChoice == 3)
{
campus = "Seaside Campus";
}
else {
cout << "Please enter a valid choice." << "\n" << "\n";
}
cout << "Student's first course: " << "\n";
getline (cin, course1);
cin.ignore();
cout << "Student's second course: " << "\n";
getline (cin, course2);
cin.ignore();
cout << "Student's third course: " << "\n";
getline (cin, course3);
cin.ignore();
do {
cout << "Is " << firstName << " " << lastName << " a senior this year? Please enter \"Y\" for yes and \"N\" for no." << "\n";
cin >> seniorPracticum;
cin.ignore();
} while (seniorPracticum != "y" && seniorPracticum != "Y" && seniorPracticum != "n" && seniorPracticum != "N");
writeToRecordsFile << studentNumber << "," << firstName << "," << lastName << "," << campus << "," << course1 << "," << course2 << "," << course3 << "," << seniorPracticum << "\n";
cout << "The student record for " << firstName << " " << lastName << " has been saved." << "\n" << "\n";
}
int main()
{
cout << "Hello there! Welcome to the student record manager. From here you can enter new student information and save it to a file!!!! (More exciting to the developer than the user)." << "\n" << "\n";
string enterAnotherRecord;
ofstream writeToRecordsFile;
writeToRecordsFile.open("cop2224_proj1.txt");
while (enterAnotherRecord != "Q" && enterAnotherRecord != "q") {
cout << "Press \"N\" to create a new student record or press \"Q\" to quit." << "\n" << "\n";
cin >> enterAnotherRecord;
closeOrNewRecordMenu(enterAnotherRecord);
string firstName, lastName, seniorPracticum, campus, course1, course2, course3;
double studentNumber;
newStudentRecord(studentNumber, firstName, lastName, campus, course1, course2, course3, seniorPracticum, writeToRecordsFile);
}
writeToRecordsFile.close();
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
作废关闭或新记录菜单(字符串输入其他记录)
{
if(enterAnotherRecord==“Q”&&enterAnotherRecord==“Q”)
{
出口(0);
}
}
void newStudentRecord(双倍学号、字符串名、字符串名、字符串校园、字符串课程1、字符串课程2、字符串课程3、字符串高级实习、流写记录文件)
{
内钟楼;
做{
学生人数;
cin.ignore();
}
而(学号<100000&&studentNumber>99999);
不能直呼其名;
cin.ignore();
库特姓氏;
cin.ignore();
而(钟楼<1 | |钟楼>3)
cout流是不可复制的,即使它们是可复制的,您也不希望在此处执行此操作–而是通过引用传递。将您的newStudentRecord
签名更改为:
void newStudentRecord(double studentNumber, string firstName, string lastName, string campus, string course1, string course2, string course3, string seniorPracticum, ofstream& writeToRecordsFile);
也就是说,当您不关心这些参数的初始值并且不将它们用作输出参数时,为什么要传入所有这些参数?请将签名简化为以下内容:
void newStudentRecord(ofstream& writeToRecordsFile);
并将其他参数声明为newStudentRecord
中的局部变量
另一方面,在初始化campusChoice之前,您正在阅读campusChoice
,这将产生