首页 > 解决方案 > C++,指针,抛出异常:读取访问冲突。这是 0xFFFFFFFFFFFFFFFF5F

问题描述

我正在做一个关于指针的学校项目。我一直在设置字符串的变量,但是当我到达时int它会抛出

Exception thrown: read access violation. this was 0xFFFFFFFFFFFFFF5F

我不确定这个特定变量age是什么问题,其余的都很好。

我试图设置int age = 0;以某种方式对其进行初始化,但没有成功。这是屏幕截图和文件,如果这有助于缩小问题范围。

在此处输入图像描述

学生.cpp

    #pragma once
    #include <string>
    #include <iostream>
    #include "student.h"
    #include "degree.h"
    using namespace std;
    
    // Accessors
    string Student::getStudent_ID()
    {
        return Student_ID;
    }
    
    string Student::getFirst_Name()
    {
        return First_Name;
    }
    
    string Student::getLast_Name()
    {
        return Last_Name;
    }
    
    string Student::getEmail_Address()
    {
        return Email_Address;
    }
    
    int Student::getAge()
    {
        return age;
    }
    
    int* Student::getNumber_Days()
    {
        return Number_Days;
    }
    
    DegreeProgram Student::getDegreeProgram()
    {
        return degreeProgram;
    }
    
    //Mutators
    
    void Student::setStudent_ID(string student_ID)
    {
        this->Student_ID = Student_ID;
    }
    
    void Student::setFirst_Name(string first_Name)
    {
        this->First_Name = First_Name;
    }
    
    void Student::setLast_Name(string last_Name)
    {
        this->Last_Name = Last_Name;
    }
    
    void Student::setEmail_Address(string email_Address)
    {
        this->Email_Address = Email_Address;
    }
    
    void Student::setAge(int age)
    {
        this->age = age;
    }
    
    void Student::setNumber_Days(int* Number_Days)
    {
        this->Number_Days[0] = Number_Days[0];
        this->Number_Days[1] = Number_Days[1];
        this->Number_Days[2] = Number_Days[2];
    }
    
    void Student::setDegreeProgram(DegreeProgram degreeProgram)
    {
        this->degreeProgram = degreeProgram;
    }
    
    //Constructor - essentually the same as a group of setters
    Student::Student(string Student_ID, string First_Name, string Last_Name, string Email_Address, int age, int Number_Days[], DegreeProgram degreeProgram)
    {
        this->Student_ID = Student_ID;
        this->First_Name = First_Name;
        this->Last_Name = Last_Name;
        this->Email_Address = Email_Address;
        this->age = age;
        this->Number_Days[0] = Number_Days[0];
        this->Number_Days[1] = Number_Days[1];
        this->Number_Days[2] = Number_Days[2];
        this->degreeProgram = degreeProgram;
    }
    
    void Student::print()
    {
        cout << Student_ID << "\t" << First_Name << "\t" << Last_Name << "\t" << age << "\t" << Number_Days[0] << "\t" << Number_Days[1] << "\t" << Number_Days[2] << "\t" << degreeProgram << endl;
    }

Student.h 文件

    #pragma once
    #include <string>
    #include <iostream>
    #include "degree.h"
    using namespace std;
    
    class Student
    {
    private:
        string Student_ID;
        string First_Name;
        string Last_Name;
        string Email_Address;
        int age;
        int* Number_Days = 0;
        DegreeProgram degreeProgram;
    
    public:
        //initialize need help
        // accessors
        Student(string Student_ID, string First_Name, string Last_Name, string Email_Address, int age, int Number_Days[], DegreeProgram degreeProgram);
    
    
        string getStudent_ID();
        string getFirst_Name();
        string getLast_Name();
        string getEmail_Address();
        int getAge();
        int* getNumber_Days();
        DegreeProgram getDegreeProgram();
    
        // mutators
    
        void setStudent_ID(string);
        void setFirst_Name(string);
        void setLast_Name(string);
        void setEmail_Address(string);
        void setAge(int);
        void setNumber_Days(int Number_Days[]);
        void setDegreeProgram(DegreeProgram degreeProgram);
    
        void print();
        ~Student();
    
    };

名册.cpp

    #pragma once
    #include <string>
    #include <iostream>
    #include <stdlib.h>
    #include "student.h"
    #include "degree.h"
    #include "roster.h"
    using namespace std;
    
    //Student* classRosterArray[5];
    
    //void add(string Student_ID, string First_Name, string Last_Name, string Email_Address, int Age, int Number_Days[], DegreeProgram degreeProgram);
    //Student* classRosterArray[5]; //string studentData() = 0;
    
    const string studentData[] =
    { "A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY",
    "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
    "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
    "A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY",
    "A5,Rebekah,Johnson,rjoh804@wgu.edu,27,35,56,65,SOFTWARE" };
    
    
    //2.E Looping through StudentData and parsing it
    //Student* classRosterArray[];
    
    void Roster::add(string Student_ID, string First_Name, string Last_Name, string Email_Address, int age, int Number_Days1, int Number_Days2, int Number_Days3, DegreeProgram degreeProgram)
    //void Roster::parsingStudentData()
    {
        for (int i = 0; i < 5; i++)
        {
            //string studentData[];
            int rhs = studentData[i].find(",");
    
            classRosterArray[i]->setStudent_ID(studentData[i].substr(0, rhs));
    
            int lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            classRosterArray[i]->setFirst_Name(studentData[i].substr(lhs, rhs - lhs));
    
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            classRosterArray[i]->setLast_Name(studentData[i].substr(lhs, rhs - lhs));
    
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            classRosterArray[i]->setEmail_Address(studentData[i].substr(lhs, rhs - lhs));
    
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            classRosterArray[i]->setAge(stoi(studentData[i].substr(lhs, rhs - lhs))); // Made the string an int with "stoi"// help
    
            //int NumberDays[Student::Number_Days];
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            int NumberDays1 = stoi(studentData[i].substr(lhs, rhs - lhs));
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            int NumberDays2 = stoi(studentData[i].substr(lhs, rhs - lhs));
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            int NumberDays3 = stoi(studentData[i].substr(lhs, rhs - lhs));
    
            int NumberDays[3] = { NumberDays1, NumberDays2, NumberDays3 };
            classRosterArray[i]->setNumber_Days(NumberDays);
    
            //Enum type
            lhs = rhs + 1;
            rhs = studentData[i].find(",", lhs);
            //TO FINISH, THIS NEEDs TO BE REFINED, WE do not have the word in between defined"studentData[i].substr(lhs, rhs - lhs)" done
            if (studentData[i] == "SECURITY")
                classRosterArray[i]->setDegreeProgram(SECURITY);
            else if (studentData[i] == "SOFTWARE")
                classRosterArray[i]->setDegreeProgram(SOFTWARE);
            else
                classRosterArray[i]->setDegreeProgram(NETWORK);
        }
    
        //void Roster::add();
    }
    
    void Roster::remove(string Student_ID)
    {
        for (int i = 0; i < 5; i = i + 1)
        {
            if (Student_ID == classRosterArray[i]->getStudent_ID())
            {
                classRosterArray[i] = nullptr;
            }
            else
            {
                cout << "Error:Student not found" << endl;
            }
        }
    }
    
    void Roster::printAll()
    {
        for (int i = 0; i < 5; i = i + 1)
        {
            this->classRosterArray[i]->print();
            //cout << Student_ID << /t << First_Name << /t << Last_Name << /t << Age << /t << Number_Days[] <<
        }
    }
    
    void Roster::printAverageDaysInCourse(string Student_ID)
    {
        for (int i = 0; i < 5; i = i + 1)
        {
            //this->classRosterArray[i]->getNumber_Days();
            //int* Number_Days[] = classRosterArray[i]->getNumber_Days();
            int AvgDays = (classRosterArray[i]->getNumber_Days()[0] + classRosterArray[i]->getNumber_Days()[1] + classRosterArray[i]->getNumber_Days()[2]) / 3;
            cout << Student_ID << ": " << AvgDays << endl;
    
        }
    }
    
    void Roster::printInvalidEmails()
    {
        for (int i = 0; i < 5; i = i + 1)
        {
            string Email_Check = classRosterArray[i]->getEmail_Address();
            string StudentID = classRosterArray[i]->getStudent_ID();
            if (Email_Check.find("@") == string::npos or Email_Check.find(".") == string::npos or Email_Check.find(" ") != string::npos)
            {
                cout << StudentID << ": Invalid Email" << endl;
            }
        }
    }
    
    void Roster::printByDegreeProgram(DegreeProgram degreeProgram)
    {
        for (int i = 0; i < 5; i = i + 1)
        {
            if (classRosterArray[i]->getDegreeProgram() == degreeProgram)
            {
                classRosterArray[i]->print();
            }
        }
    }

名册.h

    #pragma once
    #include <string>
    #include <iostream>
    #include "student.h"
    #include "degree.h"
    using namespace std;
    
    class Roster 
    {
    
        Student* classRosterArray[5]; // = { 0,0,0 }; // Array holding Student data table information
        void inicializeArray()
        {
            classRosterArray[0] = 0; // elements of the array initialized to 0
            classRosterArray[1] = 0;
            classRosterArray[2] = 0;
            classRosterArray[3] = 0;
            classRosterArray[4] = 0;
        }
    
        void parsingStudentData();
     
        
        //classRosterArray=(Student1, Student2, )
        // classRosterArray[2] = 37; // sets Student3 to 37 // to access the variables in classRosterArray
    
    
    //Roster* classRosterArray[] = { "Student_ID", "First_Name", "Last_Name", "Email_Address", " Age", " Number_Days[]", "DegreeProgram"};
    
    public:
        void add(string Student_ID, string First_Name, string Last_Name, string Email_Address, int Age, int Number_Days1, int Number_Days2, int Number_Days3, DegreeProgram degreeProgram);
        void remove(string Student_ID);
        void printAll();
        void printAverageDaysInCourse(string Student_ID);
        void printInvalidEmails();
        void printByDegreeProgram(DegreeProgram degreeProgram);
    };

标签: c++pointers

解决方案


推荐阅读