当类中包含STL容器成员时
来源:互联网 发布:vip域名备案的可能性 编辑:程序博客网 时间:2024/06/05 07:43
最近为一个低级错误犯了一下午难。
student.h
student.cpp
main.cpp
CClass类中定义了list成员变量m_lstStudent;
在使用CClass时对其进行了清零操作
ZeroMemory(&ccl, sizeof(CClass));
这样把链表申请到的内存指针_Head置为NULL,导致在使用该链表时产生访问违规。
student.h
class CStudent
{
public:
void SetName(LPCTSTR lpszName);
void SetAge(int iAge);
LPCTSTR GetName();
int GetAge();
protected:
TCHAR m_szName[MAX_NAME_LEN];
int m_iAge;
private:
};
class CClass
{
public:
CStudent* FindStudentByName(LPCTSTR lpszName);
void PushStudent(CStudent& student);
protected:
list<CStudent> m_lstStudent;
TCHAR m_szName[MAX_NAME_LEN];
private:
};
{
public:
void SetName(LPCTSTR lpszName);
void SetAge(int iAge);
LPCTSTR GetName();
int GetAge();
protected:
TCHAR m_szName[MAX_NAME_LEN];
int m_iAge;
private:
};
class CClass
{
public:
CStudent* FindStudentByName(LPCTSTR lpszName);
void PushStudent(CStudent& student);
protected:
list<CStudent> m_lstStudent;
TCHAR m_szName[MAX_NAME_LEN];
private:
};
student.cpp
CStudent* CClass::FindStudentByName(LPCTSTR lpszName) const
{
list<CStudent>::iterator it;
for (it=m_lstStudent.begin(); it!=m_lstStudent.end(); it++)
{
if (_tcsicmp(it->m_szName, lpszName) == 0)
{
return (CStudent*)it;
}
}
return NULL;
}
void CClass::PushStudent(CStudent& student)
{
m_lstStudent.push_back(student);
}
{
list<CStudent>::iterator it;
for (it=m_lstStudent.begin(); it!=m_lstStudent.end(); it++)
{
if (_tcsicmp(it->m_szName, lpszName) == 0)
{
return (CStudent*)it;
}
}
return NULL;
}
void CClass::PushStudent(CStudent& student)
{
m_lstStudent.push_back(student);
}
main.cpp
int main()
{
CClass ccl;
ZeroMemory(&ccl, sizeof(CClass));
CStudent cst;
cst.SetAge(18);
cst.SetName(_T("Yisong"));
ccl.PushStudent(cst);
return 0;
}
{
CClass ccl;
ZeroMemory(&ccl, sizeof(CClass));
CStudent cst;
cst.SetAge(18);
cst.SetName(_T("Yisong"));
ccl.PushStudent(cst);
return 0;
}
CClass类中定义了list成员变量m_lstStudent;
在使用CClass时对其进行了清零操作
ZeroMemory(&ccl, sizeof(CClass));
这样把链表申请到的内存指针_Head置为NULL,导致在使用该链表时产生访问违规。
- 当类中包含STL容器成员时
- C++类中包含stl容器时,使用sizeof求大小的问题
- STL中所有容器共有成员函数
- template类中容器成员的访问
- ALT 工程中,当导出类中包含string类型的私有成员时,会产生告警 warning C4251:class“std::basic_string”需要有 dll 接口由 class“xxx”的客户端使用
- C++包含stl容器后的大小
- STL中List容器类的使用方法
- STL中set容器
- STL中关联容器
- STL中常见容器
- STL 中容器
- 闲谈STL容器之size()成员函数
- 【STL】各容器成员对比表
- STL各容器成员对比表
- STL 各容器成员对比表
- STL各个容器成员比较表
- 【STL】各容器成员对比表
- STL常用容器成员函数列表
- IBM WAS WPS的脚本语言jacl探索
- ps命令
- 纯资源的dll文件来实现多语言
- 计算机和生命体畅想曲
- sql server 2000 局域网无法访问问题
- 当类中包含STL容器成员时
- google面试题及我的算法(1)——交叉换位(完美版)
- Filter使用详解
- servlet和JSP过滤器Filter
- 范爱农
- linux系统启动。
- 详解webconfig里面的configSections
- Excel编写VBA判断2个值是否相等,然后改变单元格颜色
- java中方法和属性的区别