I am new to C++ and would like to access the values in a subclass. When I am trying to access the values, my program is crashing and returning stack-dump.
For example:
class test{
protected:
std::string name;
int points;
object** inventory;
public:
test(const std::string name, int points) : name(name), points(points), inventory(new object*[10]()) {
for(int i = 0; i < 10; i++) {
this->inventory[i]->setValid(false);
}
}
class object {
protected:
bool isValid;
std::string name;
int value;
public:
object(const std::string name, int value) : name(name), value(value), isValid(false) {}
const std::string getName();
bool getValid();
void setValid(bool isValid);
};
In the header file.:
void object::setValid(bool isValid) {
this->isValid = isValid;
//std::cout << isValid; returning of isValid is possible, but not of this->isValid
}
The necessary header files and declarations are included. While debugging it stops while trying to get the value of this->isValid in my class object with the following error message:
Failed to execute MI command:
-data-evaluate-expression ((this)->isValid)
Error message from debugger back end:
Cannot access memory at address 0xc
Do I use an incorrect pointer? How can I solve the issue?