【问题标题】:Exception thrown: read access violation. **_Right_data** was 0x4. C++抛出异常:读取访问冲突。 **_Right_data** 为 0x4。 C++
【发布时间】:2019-10-10 03:33:54
【问题描述】:

我什么都试过了,到处找,但我一直在 抛出异常:读取访问冲突。 **_Right_data 为 0x4。**

代码有什么问题?我对 C++ 不是很好,根本不喜欢它,但在这个学校项目上工作并试图弄清楚为什么我会得到例外。任何帮助表示赞赏

#include "Roster.h"
#include "Student.h"
#include "NetworkStudent.h"
#include "SecurityStudent.h"
#include "SoftwareStudent.h"
#include <iostream>
#include <string>
#include<vector>
#include<sstream>
#include<regex>

// Separates Data on basis of ,
template <class Container>
void splitString(const std::string& str, Container& cont)
{
    char delim = ',';
    std::stringstream ss(str);
    std::string token;
    while (std::getline(ss, token, delim)) {
        cont.push_back(token);
    }
}

// Checks and returns if email is valid or not
bool Email(std::string email)
{
    const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
    return regex_match(email, pattern);
}

// Main Function
int main()
{
    Roster classRoster;
    // Data Array
    const std::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", 
};

    for (unsigned int i = 0;i < 5;i++)
    {
        std::vector<std::string> words;
        std::string s = studentData[i];
        // Splits Input
        splitString(s, words);

        // if Student belongs to Security
        if (words.at(8) == "SECURITY")
        {
            int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
            classRoster.classRosterArray[i] = new SecurityStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SECURITY);
        }

        // IF Student Belongs to Software
        else if (words.at(8) == "SOFTWARE")
        {
            int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
            classRoster.classRosterArray[i]=new SoftwareStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SOFTWARE);
        }

        // If Student Belongs to Network
        else if (words.at(8) == "NETWORK")
        {
            int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
            classRoster.classRosterArray[i] = new NetworkStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, NETWORK);
        }
    }
    std::cout << "\n---------Print All Student's Data------------\n";
    classRoster.printAll();
    std::cout << "\n---------------------------------------------\n";
    std::cout << "Invalid Emails\n";
    classRoster.printInvalidEmails();
    std::cout << "\n--------------Days in Course------------------\n";
    classRoster.printDaysInCourse("A2"); 
    std::cout << "\n--------------By Degree Program------------------";
    classRoster.printByDegreeProgram(3);
    std::cout << "\n--------------Removes A3------------------\n";
    classRoster.remove("A3"); 
    classRoster.remove("A3");
    return 0;
}

// Adds Student to Roster
void Roster::add(std::string studentID, std::string firstName, std::string lastName, std::string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, degree d)
{
    if (d == SOFTWARE)
    {
        delete classRosterArray[4];
        int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
        classRosterArray[4] = new SoftwareStudent(studentID, firstName, lastName, emailAddress, age,a, d);
    }
    else if (d == NETWORK) 
    {
        delete classRosterArray[4];
        int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
        classRosterArray[4] = new NetworkStudent(studentID, firstName, lastName, emailAddress, age, a, d);
    }

    else if (d == SECURITY)
    {
        delete classRosterArray[4];
        int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
        classRosterArray[4] = new SecurityStudent(studentID, firstName, lastName, emailAddress, age, a, d);
    }
}

// Removes Student
void Roster::remove(std::string studentID)
{
    for (unsigned i = 0;i < 5;i++)
    {
        if (classRosterArray[i]->getStudentID() == studentID){
            std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID()<<"\n";
            classRosterArray[i] = NULL;
        }
    }
    std::cout << "Student ID Does not Exist \n";
}

// Prints All Data Of Array
void Roster::printAll()
{
    for (unsigned i = 0;i < 5;i++)
    {
        std::cout << i + 1 << "\t";
        classRosterArray[i]->print();
        std::cout << "\n";
    }
}

// Prints Average Days in Course for a specific Student
void Roster::printDaysInCourse(std::string studentID)
{
    int sum = 0;
    for (unsigned i = 0;i < 5;i++)
    {
        if (classRosterArray[i]->getStudentID() == studentID)
        {
            int *p = classRosterArray[i]->getStudentDaysofCourses();
            for (unsigned int j = 0;j < 3;j++)
                sum += p[j];
            delete[]p;
            break;
        }
    }
    std::cout << sum / 3.0;
}

// Prints Invalid Emails
void Roster::printInvalidEmails()
{
    for (unsigned i = 0;i < 5;i++)
    {
        std::string email = classRosterArray[i]->getStudentEmail();
        if (!Email(email))
        {
            std::cout << classRosterArray[i]->getStudentEmail();
            //classRosterArray[i]->print();
            std::cout << "\n";
        }

    }
}


// Prints By Degree Program
void Roster::printByDegreeProgram(int degreeProgram)
{
    for (unsigned i = 0;i < 5;i++)
    {
        if (classRosterArray[i]->getDegreeProgram()==degreeProgram)
        classRosterArray[i]->print();
        std::cout << "\n";
    }
}

// Destructor
Roster::~Roster()
{
    for (unsigned i = 0; i < 5; i++)
    {
        if (classRosterArray[i]!=NULL)
            delete classRosterArray[i];
    }
}

【问题讨论】:

  • 好吧,对于初学者来说,您的 studentData 数组有 4 个元素,但您的循环尝试读取第 5 个元素。这很可能导致读取访问冲突,因为studentData[4] 指向的内存不包含有效的std::string。然后,您尝试解析该字符串并且发生了不好的事情。我没有看过你的其他代码。我强烈建议您花一些时间来teach yourself how to debug。即使知道一两个简单的调试技巧也会大大改善你的生活。
  • 抱歉,我从这里删除了该数组中的第 5 个元素,因为它包含个人信息
  • 如果您使用类似std::vector&lt;std::uniq_ptr&lt;Student&gt;&gt; 的代码,您的代码会简单得多。您可以简单地从向量中删除条目,而您的析构函数将变得微不足道。

标签: c++


【解决方案1】:

想通了!

这部分导致了错误

// Removes Student
void Roster::remove(std::string studentID)
{
    for (unsigned i = 0;i < 5;i++)
    {
        if (classRosterArray[i]->getStudentID() == studentID){
            std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID()<<"\n";
            classRosterArray[i] = NULL;
        }
    }
    std::cout << "Student ID Does not Exist \n";
}

改为

// Removes Student
void Roster::remove(std::string studentID)
{
    for (unsigned i = 0; i < 5; i++)
    {
        if (classRosterArray[i] != NULL && classRosterArray[i]->getStudentID() == studentID) {
            std::cout << "STUDENT REMOVED " << classRosterArray[i]->getStudentID() << "\n";
            delete classRosterArray[i];
            classRosterArray[i] = NULL;
            return;
        }
    }
    std::cout << "Student ID Does not Exist \n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-02
    • 2018-05-19
    • 1970-01-01
    • 2016-08-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多