Error accessing structure member inside using member function of class. Hello i am unable to figure out the runtime error i am getting.
Actually i am trying t declare a struct inside a class then using the main method i m creating a pointer object of the class then using that object i trying to access the member function which tries to initialize the struct variables . but it i not happening
class UserInformation
{
public:
struct UserInfo
{
int repu, quesCount, ansCount;
};
public:
void getInfo(int userId)
{
infoStruct.repu = userId; //here is the error but i cant figure out why
next->repu=userId;
}
void display()
{
cout<<"display";
}
UserInfo infoStruct,*next;
int date;
};
int main()
{
UserInformation *obj;
obj->display();
obj->getInfo(23);
return 0;
}
This:
is an uninitialised pointer. Attempting to call member functions on it will lead to undefined behaviour.
You could do this:
But in general, you should avoid using raw pointers and dynamically-allocated memory (i.e. from
new).