簡體   English   中英

C ++中的fstream錯誤

[英]Error with fstream in C++

我正在編寫一個簡單的程序,以輸入學生記錄並將其存儲在逗號分隔的文件中。 一切看起來都不錯,但是當我運行程序時出現錯誤:

錯誤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

這是代碼:

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

    }

流不可復制,即使它們是可復制的,您也不想在這里這樣做–而是通過引用傳遞。 將您的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內部的局部變量。


campusChoicecampusChoice ,您在初始化之前正在閱讀campusChoice ,這將產生未定義的行為

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM