首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于vector.end()非法访问,该怎么解决

2013-03-21 
关于vector.end()非法访问本帖最后由 C812414776 于 2013-03-14 11:08:15 编辑typedef std::vectorint i

关于vector.end()非法访问
本帖最后由 C812414776 于 2013-03-14 11:08:15 编辑 typedef std::vector<int> ivChainCode;  
typedef std::vector<POINT> pvLineTable;  
 
bool ToLineTable(POINT ptStart, ivChainCode ChainCode, pvLineTable *pLineTable)  
{  
    ivChainCode::iterator i, j;  
    POINT ptCurrent = { 0, 0 };  
    const POINT ptOffset[8] =  
    {  
        { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 },  
        { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 }  
    };  
    int nTable[8][8] =  
    {  
        { 0, 2, 2, 2, 2, 0, 0, 0 },  
        { 0, 2, 2, 2, 2, 3, 0, 0 },  
        { 0, 2, 2, 2, 2, 3, 3, 0 },  
        { 0, 2, 2, 2, 2, 3, 3, 3 },  
        { 1, 0, 0, 0, 0, 1, 1, 1 },  
        { 1, 3, 0, 0, 0, 1, 1, 1 },  
        { 1, 3, 3, 0, 0, 1, 1, 1 },  
        { 1, 3, 3, 3, 0, 1, 1, 1 }  
    };  
    // 清空线段表 
//pLineTable->erase(pLineTable->begin(), pLineTable->end());
  pLineTable->clear();  
cyl=1;
    ptCurrent.x = ptStart.x;  
    ptCurrent.y = ptStart.y;  
    if (ChainCode.empty())  
    {  
        // 独立点   
        pLineTable->push_back(ptCurrent);  
        pLineTable->push_back(ptCurrent);  
    }  
    else  
    {  
        // 查表法转换   
        for (i = ChainCode.begin(), j = i + 1; i != ChainCode.end()-1; i++, j++)  
        {  
            if (j == ChainCode.end())  
            {  
                j = ChainCode.begin();  
            }  


            ptCurrent.x += ptOffset[*i].x;  
            ptCurrent.y += ptOffset[*i].y;  
            switch (nTable[*i][*j])  
            {  
            case 0:  
                break;  
            case 1:  
            case 2:  
                pLineTable->push_back(ptCurrent);  
                break;  
            case 3:  
                pLineTable->push_back(ptCurrent);  
                pLineTable->push_back(ptCurrent);  
                break;  
            }  
        }  
        // 排序   
        sort(pLineTable->begin(), pLineTable->end(), CPointCompare());  
    }  
    return true;  


////////////////////////我的程序///////////////////////////////////////////
BOOL CFeatureAna::ChainToLine()
{
int cl_patternnum =  patternnum;
int i,j;//循环变量
int Cnumber;
POINT Start;
pvLineTable *CLineTable;
POINT *Barycenter;
for (i=1; i < patternnum ; i++)//
{
Cnumber=m_pattern[i].index;
ivChainCode CC(m_pattern[i].ChainCode, m_pattern[i].ChainCode + Cnumber); 
Start.x = m_pattern[i].TX;
Start.y = m_pattern[i].TY;

 ToLineTable(Start,CC, CLineTable);
}
return TRUE;
}
其中m_pattern[i].ChainCode是一个数组,大小是m_pattern[i].index
ToLineTable是CSDN上找的程序,运行到clear(),pLineTable->end(),ChainCode->end()都是非法访问,在MFC里运行,是我定义还是初始化出问题了? vector iterator
[解决办法]
第7行这个CLineTable是野指针啊,直接访问不就完蛋了嘛

BOOL CFeatureAna::ChainToLine()
{
int cl_patternnum =  patternnum;
int i,j;//循环变量
int Cnumber;
POINT Start;
pvLineTable *CLineTable;
POINT *Barycenter;
for (i=1; i < patternnum ; i++)//
{
Cnumber=m_pattern[i].index;
ivChainCode CC(m_pattern[i].ChainCode, m_pattern[i].ChainCode + Cnumber); 
Start.x = m_pattern[i].TX;
Start.y = m_pattern[i].TY;

 ToLineTable(Start,CC, CLineTable);
}
return TRUE;
}

热点排行