如何解决向量下标超出范围警告和中断的问题? [英] How to solve vector subscript out of range warning and break.?
问题描述
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
我在程序中使用了许多矢量数组,从一开始就开始推回不会造成任何问题,但是当我pop_back所有数组矢量重新开始记录时,它会导致上述问题.
再次,当我再次重新启动程序时,它运行正常,没有任何问题,但是再次经历了pop_back或清除并重新启动记录,引起了同样的警告.
如何解决这个问题?
I have used many vector arrays in my program where I start push back at the beginning does not cause me any problem but when i pop_back all the array vectors start record newly it causes above problem.
Again when i restart the program again it works fine without any problem but again undergo pop_back or clear and restart the recording its causing same warning.
How to solve this problem?
void CDemoDlg::OnBnClickedPlayrecord()
{
CString string;
string=stringPlay;
if(string == "Play")
{ m_Keys.RecordState=false;
m_Recording.EnableWindow(false);
stringPlay="Stop";
m_PlayRecord.SetIcons(IDI_ICON3, IDI_ICON3);
m_Keys.PlayRecording();
m_Recording.EnableWindow(true);
}
else
{
stringPlay="Play";
m_PlayRecord.SetIcons(NULL, NULL);
m_Keys.RecordState=false;
}
OnEndOfPlayingFile();
m_Keys.SetFocus();
return;
}
上面的代码实际上引起了一些问题.
仅当调用上述函数并尝试重新开始记录时,才发出警告.
the above code is actually causing some problem.
Only when above function is called and try to restart recording only the warning is coming.
void CMIDIKeyboard::PlayRecording()
{
int KDownI=0;
int KUpI=0;
int MDwnI=0;
int MUpI=0;
int MmoveI=0;
PreviousTime=Current;
GetSystemTime(&Current);
WORD el=(Current.wMilliseconds+Current.wSecond*1000+Current.wMinute*60000+Current.wHour*3600000)-
(PreviousTime.wMilliseconds+PreviousTime.wSecond*1000+PreviousTime.wMinute*60000+PreviousTime.wHour*3600000);
ElapsedTime.push_back(el);
Event.push_back(5);
int nTotalRecords=ElapsedTime.size();
for(int i=0;i<=nTotalRecords;i++)
{
if(i==nTotalRecords)
{
return;
}
XSleep(ElapsedTime[i]);
if(Event[i]==0)
{
if(KeyDown[KDownI]==true)
{
OnKeyDown(m_KeysRecordDwnChar[KDownI],m_KeysRecordDwnRepCnt[KDownI],m_KeysRecordDwnFlags[KDownI]);
KDownI++;
}
}
if(Event[i]==1)
{
if(KeyUp[KUpI]==true)
{
OnKeyUp(m_KeysRecordUpnChar[KUpI],m_KeysRecordUpnRepCnt[KUpI],m_KeysRecordUpnFlags[KUpI]);
KUpI++;
}
}
if(Event[i]==2)
{
if(MouseLDown[MDwnI]==true)
{
OnLButtonDown(MouseRecordLDwnFlags[MDwnI],MouseRecordLDwnPoint[MDwnI]);
MDwnI++;
}
}
if(Event[i]==3)
{
if(MouseLUp[MUpI]==true)
{
OnLButtonUp(MouseRecordLUpFlags[MUpI],MouseRecordLUpPoint[MUpI]);
MUpI++;
}
}
if(Event[i]==4)
{
if(MouseDrgMove[MmoveI]==true)
{
OnMouseMove(MouseRecordMoveFlags[MmoveI],MouseRecordMovePoint[MmoveI]);
MmoveI++;
}
}
else
{
/* Do nothingg*/
}
}
}
void CMIDIKeyboard::ClearRecording()
{
m_KeysRecordDwnChar.clear();
m_KeysRecordDwnRepCnt.clear();
m_KeysRecordDwnFlags.clear();
m_KeysRecordUpnChar.clear();
m_KeysRecordUpnRepCnt.clear();
m_KeysRecordUpnFlags.clear();
MouseRecordLDwnFlags.clear();
MouseRecordLDwnPoint.clear();
MouseRecordLUpFlags.clear();
MouseRecordLUpPoint.clear();
MouseRecordMoveFlags.clear();
MouseRecordMovePoint.clear();
ElapsedTime.clear();
Event.clear();
KeyUp.clear();
KeyDown.clear();
MouseLUp.clear();
MouseLDown.clear();
MouseDrgMove.clear();
}
std::vector<UINT> m_KeysRecordDwnChar;
std::vector<UINT> m_KeysRecordDwnRepCnt;
std::vector<UINT> m_KeysRecordDwnFlags;
std::vector<UINT> m_KeysRecordUpnChar;
std::vector<UINT> m_KeysRecordUpnRepCnt;
std::vector<UINT> m_KeysRecordUpnFlags;
std::vector<UINT> MouseRecordLDwnFlags;
std::vector<CPoint> MouseRecordLDwnPoint;
std::vector<UINT> MouseRecordLUpFlags;
std::vector<CPoint> MouseRecordLUpPoint;
std::vector<UINT> MouseRecordMoveFlags;
std::vector<CPoint> MouseRecordMovePoint;
SYSTEMTIME Current;
SYSTEMTIME PreviousTime;
std::vector<WORD> ElapsedTime;
std::vector<WORD> Event;//Event=0 if keydown//Event=1 if keyUp//Event=2 if mouse down//Event=3 if Mouse up
bool RecordState;
std::vector<bool> KeyUp;
std::vector<bool> KeyDown;
std::vector<bool> MouseLUp;
std::vector<bool> MouseLDown;
std::vector<bool> MouseDrgMove;
上面的变量在MIDIKeyboard.h中,如果我将它们设为私有,那么没有其他函数可以更改其值,这可能会有所帮助,但是我在其他CDemodlg类中使用了上面的变量,如何使其变为私有,并仍由CDemoDlg和CMIDIKeyboard使用
The above variables are in MIDIKeyboard.h and If i make them private so no other function should change the values it may help but i have the above variables i am using in other CDemodlg class how to make it private and still use by CDemoDlg and CMIDIKeyboard only?
推荐答案
问题的可能根源(您未显示初始化):
The possible source of your problem (you did not show the initialization):
if(KeyDown[KDownI]==true)
当KeyDown
向量为空时,这将失败.也适用于所有其他使用的向量.
另一个错误:
This will fail when the KeyDown
vector is empty. Applies also to all other used vectors.
Another error:
WORD el=(Current.wMilliseconds+Current.wSecond*1000+Current.wMinute*60000+Current.wHour*3600000)- (PreviousTime.wMilliseconds+PreviousTime.wSecond*1000+PreviousTime.wMinute*60000+PreviousTime.wHour*3600000);
在此处使用WORD
会产生意外的结果.值不适合WORD
.
不是错误,但是风格很差.这个
Using WORD
here will give unexpected results. The values does not fit in a WORD
.
Not an error, but bad style. This
for(int i=0;i<=nTotalRecords;i++)
{
if(i==nTotalRecords)
{
return;
}
// loop code
}
应该替换为:
should be replaced by:
for(int i=0;i<nTotalRecords;i++)
{
// loop code
}
void CMIDIKeyboard::PlayRecording()
{
int KDownI=0;
int KUpI=0;
int MDwnI=0;
int MUpI=0;
int MmoveI=0;
int nTotalRecords=ElapsedTime.size();
for(int i=0;(i<nTotalRecords)&&(((CDemoDlg*)GetParent())->SongPlayrec==true);i++)
{
if(i>0)
XSleep(ElapsedTime[i]);
if((Event[i]==0)&&(((CDemoDlg*)GetParent())->SongPlayrec==true))
{
if(KeyDown[KDownI]==true)
{
OnKeyDown(m_KeysRecordDwnChar[KDownI],m_KeysRecordDwnRepCnt[KDownI],m_KeysRecordDwnFlags[KDownI]);
KDownI++;
}
}
if((Event[i]==1)&&(((CDemoDlg*)GetParent())->SongPlayrec==true))
{
if(KeyUp[KUpI]==true)
{
OnKeyUp(m_KeysRecordUpnChar[KUpI],m_KeysRecordUpnRepCnt[KUpI],m_KeysRecordUpnFlags[KUpI]);
KUpI++;
}
}
if((Event[i]==2)&&(((CDemoDlg*)GetParent())->SongPlayrec==true))
{
if(MouseLDown[MDwnI]==true)
{
OnLButtonDown(MouseRecordLDwnFlags[MDwnI],MouseRecordLDwnPoint[MDwnI]);
MDwnI++;
}
}
if((Event[i]==3)&&(((CDemoDlg*)GetParent())->SongPlayrec==true))
{
if(MouseLUp[MUpI]==true)
{
OnLButtonUp(MouseRecordLUpFlags[MUpI],MouseRecordLUpPoint[MUpI]);
MUpI++;
}
}
if((Event[i]==4)&&(((CDemoDlg*)GetParent())->SongPlayrec==true))
{
if(MouseDrgMove[MmoveI]==true)
{
OnMouseMove(MouseRecordMoveFlags[MmoveI],MouseRecordMovePoint[MmoveI]);
MmoveI++;
}
}
else
{
/* Do nothingg*/
}
cout(i);
}cout("end of for loop");
}
这篇关于如何解决向量下标超出范围警告和中断的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!