使用类的成员函数访问内部的结构成员时出错
Error accessing structure member inside using member function of class
使用类的成员函数访问内部结构成员时出错。你好,我无法找出我遇到的运行时错误
实际上,我试图在类内声明一个结构,然后使用主方法创建该类的指针对象,然后使用该对象访问试图初始化结构变量的成员函数。但不会发生
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;
}
这:
UserInformation *obj;
是一个未初始化的指针。试图在其上调用成员函数将导致未定义的行为。
你可以这样做:
UserInformation *obj = new UserInformation();
...
delete obj; // Remember to clean up!
但一般来说,您应该避免使用原始指针和动态分配的内存(即来自new
)。
相关文章:
- 如何在C++中定义静态成员结构
- 使用模板化类的成员结构
- 初始化固定的 C 数组成员结构
- 指向成员结构的指针如何工作
- 如何将此“指针从外部类传递到成员结构
- 导致IAR ARM中出现错误的成员结构位字段元素的Initializer列表初始化
- 映射文件中成员结构的地址
- Visual C++ 模板类成员结构初始化语法糖
- C :初始化成员结构的静态字段的正确方法
- 如何在C 类的初始化器列表中使用未命名结构初始化成员结构
- 从成员结构的成员函数中访问类的成员?
- 模板化类的成员函数无法返回指向成员结构的指针?
- 重载非类型模板结构的成员结构的复制赋值运算符
- 灵气属性传播问题与单成员结构.
- 将具有 int* 成员C++结构编接到 C#
- 复杂的班级成员结构
- 将向量成员(结构)传递给函数
- 成员结构的前向声明
- 重载运算符<<用于模板结构中定义的成员结构
- 返回带有模板化类的类成员结构