Вызвано исключение: нарушение доступа для чтения. _Val было 0xFFFFFFFFFFFFFFFF
не понимаю, в чем дело. Уже всё перечитал и перепробовал.
С классом Driver всё в порядке, а при вызове конструктора Autos выдается данная ошибка. Когда классы были раздельными, но реализованы одинаково, то ошибок не возникало.
***Autos.h***
#pragma once
#include <iostream>
class Driver
{
protected:
std::string* NAME = nullptr;
int* ID = nullptr;
bool Status = false;
private:
static int count;
public:
Driver() {
NAME = new std::string("None");
ID = new int(count);
count++;
}
Driver(std::string name) {
NAME = new std::string(name);
ID = new int(count);
count++;
}
std::string GetName() {
return *NAME;
}
bool GetStatus() {
return Status;
}
void SetStatus(bool IsBusy) {
this->Status = IsBusy;
}
void SetName(std::string a) {
*this->NAME = a;
}
void ShowInfo() {
std::cout << *NAME << " " << *ID <<std::endl;
}
void ChangeName(std::string NewName) {
delete this->NAME;
this->NAME = new std::string(NewName);
}
void ChangeId(int NewId) {
delete this->ID;
this->ID = new int(NewId);
}
~Driver() {
delete NAME;
delete ID;
}
};
int Driver::count = 0;
class Autos : Driver
{
private:
std::string* DriverName = nullptr;
static int AutosCount;
введите сюда код
public:
Autos() {
NAME = new std::string("None");
ID = new int(AutosCount);
AutosCount++;
}
Autos(std::string Name) {
NAME = new std::string(Name);
ID = new int(AutosCount);
AutosCount++;
}
Autos(std::string name, Driver &SomeDriver) {
ID = new int(AutosCount);
NAME = new std::string(name);
SomeDriver.SetStatus(true);
*this->DriverName = SomeDriver.GetName();
AutosCount++;
}
void AttachDriver(Driver &SomeDriver) {
if (SomeDriver.GetStatus()) {
throw std::exception("Driver is busy");
}
*this->DriverName = SomeDriver.GetName();
SomeDriver.SetStatus(true);
}
void DeattachDriver() {
if (this->DriverName == nullptr) {
throw std::exception("There is no driver attached to this bus.");
}
}
std::string GetDriverName() {
return *this->DriverName;
}
void ShowInfo() {
std::cout << "Bus name: " << *this->NAME << " with id: " << *this->ID << " IsOnline: " << this->Status
<< "\nDriver name: " << this->DriverName << std::endl;
}
~Autos() {
delete NAME;
delete ID;
}
};
int Autos::AutosCount = 0;
"main.cpp"
#include "Autos.h"
#include <clocale>
int main()
{
setlocale(LC_ALL,"Rus");
//Autos Paz("Paz");
Driver Max("Max");
Auto
s Paz; }
Источник: Stack Overflow на русском