十万火急:求教用C++的find泛型函数查找类向量中元素的问题?
如题,调试也好久,没发觉有什么错误,要查找的类实例的名称为Block,在find函数调用中对类实例指针向量查找某一该类实例指针对象,结果编译运行的时候报错:3D俄罗斯方块触发了一个断点.通过单步调试发觉是find调用的问题,以下是类的源代码和定义对象指针的源代码和绘制场景时的函数调用的源代码(其中含有OpenGL的东西我就不贴出来了),在线等,谢谢.
#ifndef BLOCK_H
#define BLOCK_H
#include "define.h"
//-----------------------
// 方块类
//-----------------------
class Block
{
private:
int m_iRenderList; // 方块的显示列表对象
unsigned int m_uiTextureIdentifier; // 所使用的纹理对象标识名称
int m_cx; // x轴坐标
int m_cy; // y轴坐标
public:
//---------Block---------
// 描述:无参数构造函数
// 参数;无
//-----------------------
Block(){}
//---------Block-------------------
// 描述:带参数构造函数.用于在
// 方块对象指针中进行检索
// 参数;x - 此方块在游戏区域的行号
// y - 此方块在游戏区域的列号
//---------------------------------
Block(int x,int y):m_iRenderList(-1),m_cx(x), m_cy(y){}
//---------Block------------------------
// 描述:带参数构造函数,初始化各成员
// 变量并定义方块绘制显示列表
// 参数;textureName - 纹理对象标识
// x - 此方块在游戏区域的行号
// y - 此方块在游戏区域的列号
//--------------------------------------
Block(unsigned int textureName,
int x,
int y):m_iRenderList(-1),
m_uiTextureIdentifier(textureName),
m_cx(x), m_cy(y)
{
BuildList(); // 构建绘制显示列表
}
//---------~Block------------------
// 描述:析构函数,删除显示列表对象
// 参数;无
//---------------------------------
~Block()
{
if(m_iRenderList != -1)
{
glDeleteLists(m_iRenderList,1); // 删除显示列表对象
}
}
// 重载"=="运算符:用于对象指
// 针向量的检索作为比较的标准
bool operator==(Block* &pBlock) const
{
return (m_cx == pBlock->GetXCoordinate() && m_cy == pBlock->GetYCoordinate());
}
// 构建绘制显示列表
void BuildList();
//---------Render--------
// 描述:绘制方块
// 参数;无
// 返回:无
//-----------------------
void Render()
{
// 调用显示列表对方块进行绘制
glCallList(m_iRenderList);
}
//---------GetXCoordinate--------
// 描述:得到X坐标值
// 参数;无
// 返回:此方块的X坐标值
//-------------------------------
int GetXCoordinate()
{
return m_cx;
}
//---------GetYCoordinate--------
// 描述:得到Y坐标值
// 参数;无
// 返回:此方块的Y坐标值
//-------------------------------
int GetYCoordinate()
{
return m_cy;
}
//---------GetTextureIdentifier--------
// 描述:得到纹理标识
// 参数;无
// 返回:此方块的纹理标识
//-------------------------------------
unsigned int GetTextureIdentifier()
{
return m_uiTextureIdentifier;
}
};
#endif
初始化渲染状态:
GLvoid Control::ReSizeGLScene(GLsizei width, GLsizei height)
{
Block* tempBlock = new Block(BlockTextureIndetify[0],0,0);
m_vecGameBlocks.push_back(tempBlock);
}
绘制场景函数调用中涉及此问题的代码:
bool Control::DrawGLScene()
{
vector<Block*>::iterator itTemp;
Block* pBlock1 = new Block(0,0);
itTemp = find(m_vecGameBlocks.begin(),m_vecGameBlocks.end(),pBlock1);
(*itTemp)->Render();
}
[解决办法]
bool Control::DrawGLScene()
{
vector <Block* >::iterator itTemp;
Block* pBlock1 = new Block(0,0);
itTemp = find(m_vecGameBlocks.begin(),m_vecGameBlocks.end(),pBlock1);//这里找的是指针值,你又没有把这个指针添加到vector里,这里当然找不到。
if(itTemp != m_vecGameBlocks.end())
{
(*itTemp)- >Render();
}
}
[解决办法]
刚才不好意思,find没仔细看,看下面代码
vector <Block* >::iterator itTemp;
Block* pBlock1 = new Block(0,0);
itTemp = find(m_vecGameBlocks.begin(),m_vecGameBlocks.end(),pBlock1);
(*itTemp)- >Render();
我觉得这段代码有问题:find函数是在制定vector内查找特定项,也就是说pBlock1地址事先要存在,而显然这个pBlock1是临时new的新地址,那么显然itTemp的不到正确的返回值,所以错误产生在(*it)->render()因为返回的it是错误值,所以此指针非法操作,导致错误.而你说find这句就错误,应该不可能,因为find函数不会无故中断.我写了一个测试程序,请看:
int test[2]={1, 2};
vector <int* > _vec;
vector <int* >::iterator it;
_vec.push_back(test);
int * pv = new int(0);
it = find(_vec.begin(), _vec.end(), pv);
int a = *(*it);
system("pause");
此时执行到int a =...出错,因为it返回值错误,但是把it = find(....pv)改为it=find(,..,test)则正确.