我有三个类,一个函数在代码中的某个位置运行正常,如果我把它放在其他地方崩溃,我无法弄明白,为什么会发生。我会很高兴前期指导。
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
....
,我得到:
访问冲突读取位置0x00000000
我很抱歉,如果代码太久了,我很长时间都在努力...
谢谢。
您在此行中有作业:
if (p_pipeline[index] = NULL)
而不是比较
if (p_pipeline[index] == NULL)
这就是你访问地址0x00000000
的原因