Answer the question
In order to leave comments, you need to log in
How to solve the access violation error when reading from an address?
Hello, I encountered a runtime error that says the following:
Вызвано исключение по адресу 0x007C3AD9 в home_oop_inheritance_virtual_metod_spd_animal.exe: 0xC0000005: нарушение прав доступа при чтении по адресу 0x00000000.
void printAll(){
for (size_t i = 0; i < zoo.size() + 1; i++)
{
zoo[i]->show();// ошибку выдает здесь
cout << endl;
}
}
class Animal
{
protected:
string name = "NoName";
size_t age = 1;
public:
Animal(const string& name, const size_t& age)
:name(name), age(age) {}
Animal() = default;
virtual void sound() const = 0;
virtual void type() const = 0;
virtual void show() const = 0;
string getName() {
return this->name;
}
size_t getAge() {
return this->age;
}
void setName(const string& newName) {
if (newName != "")
{
name = newName;
return;
}
return;
}
void setAge(const size_t newAge) {
if (newAge >= 0)
{
age = newAge;
return;
}
return;
}
};
for (size_t i = 0; i < zoo.size() - 1; i++)
{
zoo[i]->type();
cout << " Name: " << zoo[i]->getName() << " Age: " << zoo[i]->getAge() << " ";
zoo[i]->sound();
cout << endl;
}
Answer the question
In order to leave comments, you need to log in
Well, a guaranteed flight beyond the array boundary is here. i < zoo.size() + 1
What does +1 do here?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question