i have the indicated problem with the following code and i have no idea what it might be causing it. I searched before posting the problem, and i learned that it might be something going out of the scope like a reference to a freed memory location but i could not find it on my own.
Thank you for helping me.
#include<iostream>
#include<string>
using namespace std;
class USR{
private:
string name;
public:
void setName(string name){
this->name = name;
}
string getName(){
return name;
}
};
class A{
private:
USR* * a;
public:
A(int size){
a = new USR*[size];
}
USR* getUser(){
return a[0];
}
};
int main(){
A test = A(5);
USR* u = test.getUser();
(*u).setName("test");
USR* u2 = test.getUser();
cout << (*u2).getName() << endl;
cout << (*u).getName() << endl;
}
Your method
getUserreturns an uninitialized pointer (the constructor ofAcreates an array of uninitialized pointers). The error you see is the result of dereferencing the uninitialized pointer that method returned.