未处理的异常 - 访问冲突读取位置0x00000000

时间:2015-01-08 13:00:26

标签: c++ polymorphism

我有三个类,一个函数在代码中的某个位置运行正常,如果我把它放在其他地方就崩溃了,我无法弄明白,为什么会发生这种情况。我很乐意在前面指导。

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;   
     }
 }
}

使用此命令序列可以正常运行代码,但如果我更改了有问题的行&#39;到其他地方:

     ....
     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

如果代码太长,我很抱歉,我很长时间都在努力...

感谢。

1 个答案:

答案 0 :(得分:3)

您在此行中有作业:

if (p_pipeline[index] = NULL)

而不是比较

if (p_pipeline[index] == NULL)

这就是您访问地址0x00000000

的原因