How can I get my queue in C++ work? Unhandeled Exception: Access violation reading location

0

I got a problem by implementing a queue in C++. I looked for similar problems, but didn't find anything usefull.

I'm using Visual Studio 2019.

I seperated my program in a Main.cpp, a Queue.h and Queue.cpp, Patient.h and Patient.cpp. I tried to convert the concept for this from Java to C++, but I just can't find a solution for my function getInfo().

I get an exception like this: Unhandled exception at 0x7C0EF3BE (ucrtbased.dll) in Queue.exe: 0xC0000005: Access violation reading location 0xE8884D8D.

Would be nice if anyone could help me with my problem and explain what I did wrong. I'm just a beginner so don't be too harsh on me pls xD

Main.cpp:

#include "Queue.h"
#include "Patient.h"

int main() {
    Queue queue;
    Patient patient1("Name1");
    Patient patient2("Name2");
    queue.add(patient1);
    queue.add(patient2);
    queue.getInfo();
}

Queue.h:

#pragma once

#include <iostream>
#include "Patient.h"

using namespace std;

class Queue {

private:
    Patient* beginning;
    Patient* end;
    int amount;

public:
    Queue();
    void add(Patient p);
    Patient remove();
    void getInfo();
};

Queue.cpp:

#include "Queue.h"

Queue::Queue() {
    beginning = 0;
    end = 0;
    amount = 0;
}

void Queue::add(Patient p) {
    if (amount == 0) {
        beginning = &p;
        end = &p;
    } else {
        end->setFollower(p);
        end = &p;
    }
    amount++;
}

Patient Queue::remove() {
    if (amount == 0) {
        cout << "You can't remove a patient. The Queue is empty!" << endl;
    } else {
        *beginning = beginning->getFollower();
        amount--;
    }
    return *beginning;
}

void Queue::getInfo() {
    if (amount == 0) {
        cout << "The Queue is empty!" << endl;
    } else {
        cout << "There are " << amount << " Patients in the Queue!" << endl;
        cout << "The following list provides all Patients in the Queue-order:" << endl;
        beginning->getInfo();
    }
}

Patient.h:

#pragma once

#include <iostream>
#include <string>

using namespace std;

class Patient {

private:
    string name;
    Patient* follower;
    string* nameptr;

public:
    Patient(string newname);
    void setFollower(Patient p);
    Patient getFollower();
    void getInfo();
};

Patient.cpp:

#include "Patient.h"

    Patient::Patient(string newname) {
        name = newname;
        follower = 0;
        nameptr = &name;
    }

    void Patient::setFollower(Patient p) {
        follower = &p;
    }

    Patient Patient::getFollower() {
        return *follower;
    }

    void Patient::getInfo() {
        cout << *nameptr << endl;
        if (follower == 0) {
            cout << "No follower existing!" << endl;
        }
        else {
            follower->getInfo();
        }
        cin.get();
    }
exception
visual-c++
asked on Stack Overflow Jul 27, 2020 by Future

1 Answer

0

There are a few places where you mix up passing by value with passing by reference.

To start with, the first problem is here:

void Queue::add(Patient p) {
    if (amount == 0) {
        beginning = &p;
        end = &p;
    } else {
        end->setFollower(p);
        end = &p;
    }
    amount++;
}

You are passing the value of Patient p rather than a reference to the actual object. To fix this you only need to add an "&" to your functional call like this:

void Queue::add(Patient& p) {
    if (amount == 0) {
        beginning = &p;
        end = &p;
    } else {
        end->setFollower(p);
        end = &p;
    }
    amount++;
}

Note the "&" in the parameters list. Then you must also update the function header:

class Queue {

private:
    Patient* beginning;
    Patient* end;
    int amount;

public:
    Queue();
    void add(Patient& p);
    Patient remove();
    void getInfo();
};

You must also pass by reference for your setFollower function:

void Patient::setFollower(Patient& p) {
    follower = &p;
}

and in the header file:

void setFollower(Patient& p);

What you need to know going forward is that in C++ all arguments are passed by value unless you specify passing by reference in the function's parameter list. Here is an article about passing variables to functions if you'd like to read more (https://iq.opengenus.org/call-by-value-vs-call-by-reference-cpp/).

answered on Stack Overflow Jul 27, 2020 by TheWriter

User contributions licensed under CC BY-SA 3.0