2015-01-08 48 views
-2

我有三個類,一個函數可以在代碼中的某個地方正常運行,如果我把它放在其他地方並且我無法弄清楚,爲什麼會發生這種情況。我會很樂意提前指導。未處理的異常 - 訪問違規讀取位置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

我很抱歉,如果代碼太長,我很長時間的努力..

謝謝。

+1

使用向量和智能指針,我敢打賭你的問題將消失..'std :: vector >> aray;'值得努力學習這是什麼意思和如何使用它。 –

+0

我認爲這個錯誤可能在'checkIfNewFilterCreated()'的某個地方,你可以發佈一些代碼嗎? – Kuba

+0

「p_pipeline」定義在哪裏? –

回答

3

你必須分配在這一行:

if (p_pipeline[index] = NULL)

代替comparisson

if (p_pipeline[index] == NULL)

這就是爲什麼你要訪問地址0x00000000

+0

我希望編譯器將這個警告提升爲錯誤,這太容易了。 – sjdowling

相關問題