简体   繁体   English

C ++中的fstream错误

[英]Error with fstream in C++

I'm writing a simple program to enter student records and store them in a coma delimited file. 我正在编写一个简单的程序,以输入学生记录并将其存储在逗号分隔的文件中。 Everything looks good but when I run the program I get an error: 一切看起来都不错,但是当我运行程序时出现错误:

Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>' c:\\program files\\microsoft visual studio 10.0\\vc\\include\\fstream 1116 1 project1 错误1错误C2248:'std :: basic_ios <_Elem,_Traits> :: basic_ios':无法访问在类'std :: basic_ios <_Elem,_Traits>中声明的私有成员'c:\\ program files \\ Microsoft Visual Studio 10.0 \\ vc \\ include \\ fstream 1116 1项目1

Here's the code: 这是代码:

#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();

    }

Streams are not copyable, and even if they were, you wouldn't want to do so here – pass by reference instead. 流不可复制,即使它们是可复制的,您也不想在这里这样做–而是通过引用传递。 Change your newStudentRecord signature to: 将您的newStudentRecord签名更改为:

void newStudentRecord(double studentNumber, string firstName, string lastName, string campus, string course1, string course2, string course3, string seniorPracticum, ofstream& writeToRecordsFile);

That being said, why are you passing in all those arguments when you don't care about their initial values and you don't use them as output parameters? 话虽这么说,当您不关心它们的初始值并且不将它们用作输出参数时,为什么还要传递所有这些参数呢? Simplify your signature to the following: 将您的签名简化为以下内容:

void newStudentRecord(ofstream& writeToRecordsFile);

and declare the other arguments as local variables inside of newStudentRecord . 并将其他参数声明为newStudentRecord内部的局部变量。


As an aside, you're reading campusChoice before initializing it, which will yield undefined behavior . campusChoicecampusChoice ,您在初始化之前正在阅读campusChoice ,这将产生未定义的行为

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM