Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Access violation executing in classes with inheritance/ c++

I have a base class Participant and I need to sort object in array of participant by quantity of their prizes or diplomas. The virtual function get_data return this number. But whileI try to sort, i’ve got error Access violation executing in row with comparing 2 numbers.
if (p[j].Get_Data() < p[i].Get_Data()) {
This is my full code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Participant {
protected:
    string name;
    string surname;
    vector <string> type;
    int age;
public:
    Participant() : name(""), surname(""), type(), age(0)
    {}
    Participant(string name, string surname, vector <string> type, int age) {
        this->name = name;
        this->surname = surname;
        this->type = type;
        this->age = age;
    };
    Participant(const Participant& other) : name(other.name), surname(other.surname), type(other.type), age(other.age)
    {
    }
    Participant& operator=(const Participant& other)
    {
        name = other.name;
        surname = other.surname;
        type = other.type;
        age = other.age;
        return *this;
    }
    virtual int Get_Data() {
        return 0;
    }
    friend void Sort(Participant* p);
};

class Diploma : public Participant {
protected:
    vector<int> places;
    vector <string> type_d;
public:
    Diploma() : places(), type_d{}
    {}
    Diploma(string name, string surname, vector <string> type, int age, vector<int> places, vector <string> type_d);
    int Get_Data() override {
        cout << "diplom prize" << endl;
        cout << type_d.size() << endl;
        return type_d.size();
    }
};
class Prize : public Participant {
protected:
    vector <int> places;
    vector<string> prize;
public:
    
    Prize(string name, string surname, vector <string> type, int age, vector<int> places, vector<string> prize);
    int Get_Data() override{
        cout << "Cont prize" << endl;
        cout << prize.size() << endl;
        return prize.size();
    }
};
Prize::Prize(string name, string surname, vector <string> type, int age, vector<int> places, vector<string> prize) {
    this->name = name;
    this->surname = surname;
    this->type = type;
    this->age = age;
    this->places = places;
    this->prize = prize;
}
Diploma::Diploma(string name, string surname, vector <string> type, int age, vector<int> places, vector <string> type_d){
    this->name = name;
    this->surname = surname;
    this->type = type;
    this->age = age;
    this->places = places;
    this->type_d = type_d;

}
void Sort(Participant* p) {
    Participant temp;
    for (int i = 0; i < 3; i++) {
        for (int j = i + 1; j < 3; j++)
        {
            if (p[j].Get_Data() < p[i].Get_Data()) {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }

    }
}

int main() {
    Participant* pa[3];
    pa[0] = new Diploma("Alex", "Smith", { "Geo","Math" }, 17, { 1,6 }, { "first"});
    pa[1] = new Prize("Helen", "Blink", { "IT","Math" }, 18, { 2,2 }, {"Golden medal", "medal"});
    pa[2] = new Prize("Brandon", "Brown", { "IT","Math" }, 18, { 2,2 }, { "Golden medal", "medal","gold"});
    Sort(*pa);
    for (int i = 0; i < 3; i++) {
        pa[i]->Get_Data();
        cout << endl;
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The issue here is that when you are passing *pa, only the first element is accessible. If you run it in debug mode you will be able to see that inside sort function, p[1] is not a valid object. Basically, only p[0] is passed to the function. You should pass the reference to the array i.e. **pa to the sort function

void Sort(Participant **p)
{
    Participant *temp;
    for (int i = 0; i < 3; i++)
    {
        for (int j = i + 1; j < 3; j++)
        {
            if (p[j]->Get_Data() < p[i]->Get_Data())
            {
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
        }
    }
}

int main()
{
    Participant *pa[3];
    pa[0] = new Diploma("Alex", "Smith", {"Geo", "Math"}, 17, {1, 6}, {"first"});
    pa[1] = new Prize("Helen", "Blink", {"IT", "Math"}, 18, {2, 2}, {"Golden medal", "medal"});
    pa[2] = new Prize("Brandon", "Brown", {"IT", "Math"}, 18, {2, 2}, {"Golden medal", "medal", "gold"});
    Sort(pa);
    for (int i = 0; i < 3; i++)
    {
        pa[i]->Get_Data();
        cout << endl;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading