我有三個類,一個函數可以在代碼中的某個地方正常運行,如果我把它放在其他地方並且我無法弄清楚,爲什麼會發生這種情況。我會很樂意提前指導。未處理的異常 - 訪問違規讀取位置0x00000000
class BaseClass
{
friend class B;
protected:
string m_name;
BaseClass::BaseClass(); // implementation doesn't matter
virtual bool execute (SRV *p_Srv) = 0;
virtual void setName(string name)
{
m_name = name;
}
~BaseClass(void); // implementation doesn't matter
};
class derivedClass:public BaseClass
{
friend class B;
protected:
derivedClass(void); // implementation doesn't matter
bool execute (SRV *p_Srv); // implementation doesn't matter
~derivedClass(void); // implementation doesn't matter
};
class B
{
BaseClasse **array;
string twoDimArray[2][MAX_PARAMS_SIZE];
bool function()
{
....
p_pipeline[i] = new derivedClass(twoDimArray);
** EDIT: array[i]->setName("name"); ** <------ problematic line
p_pipeline[i]->setName("name"); <------ problematic line
if (checkIfNewFilterCreated(i, "name") == "-1")
throw msg;
....
}
string B::checkIfNewFilterCreated(int index, string name)
{
if (p_pipeline[index] = NULL)
return "-1";
else
{
m_numOfFiltersCreated++ ;
return name;
}
}
}
代碼運行正常與命令的這個序列,但如果我改變「問題的行」到其他地方:
....
p_pipeline[i] = new derivedClass(twoDimArray);
** EDIT: array[i] = new derivedClass(twoDimArray); **
if (checkIfNewFilterCreated(i, "name") == "-1")
throw msg;
p_pipeline[i]->setName("name"); <------ problematic line
** EDIT: array[i]->setName("name"); ** <------ problematic line
....
,我得到Access violation reading location 0x00000000
我很抱歉,如果代碼太長,我很長時間的努力..
謝謝。
使用向量和智能指針,我敢打賭你的問題將消失..'std :: vector >> aray;'值得努力學習這是什麼意思和如何使用它。 –
我認爲這個錯誤可能在'checkIfNewFilterCreated()'的某個地方,你可以發佈一些代碼嗎? – Kuba
「p_pipeline」定義在哪裏? –