2013-06-02 74 views
0

我只是對我遇到的錯誤有點困惑。我製作了一個名爲「SensorNode」的類,每個SensorNode都有一個傳感器鏈表。 SensorNode的一個數據成員是名爲mySensors的SensorBlock(鏈接列表)指針。 mySensors應該指向的是在傳感器節點擁有傳感器的鏈表中的第一傳感器下面是SensorNode類聲明:如何添加一些東西到鏈接列表

class SensorNode { 
    char* NodeName; 
    int NodeID; 
    LOCATION Node1; 
    float batt; 
    int func; 
    SensorBlock *mySensors; 


public: 
    SensorNode(char *n, float x, float y, float z, int i, float ah); 
    void print(); 
    void setOK(int o); 
    int getOK(); 
    void setLOC(float longi, float lat, float h); 
    int amIThisSensorNode(char *n); 
    void addSensorToNode(sensor *s); 
}; 

,這裏是爲SensorBlock類聲明:

class SensorBlock { 

    friend class SensorNode; 
    SensorBlock * LLelement; 
    sensor * SensEl; 
}; 

我的問題是我的void addSensorToNode(sensor * s)函數。參數s指向應該添加到屬於該節點的傳感器列表末尾的傳感器。我不知道該怎麼做,因爲它不是我創建和添加的新傳感器,而是一個指向我添加的傳感器的指針。

這是我到目前爲止有:

void SensorNode::addSensorToNode(sensor *s) { 
    if(mySensors == '\0') //mySensors is first initialized to NULL 
    { 
     mySensors = s; //I get an error on this line. 
    } 
    else { 

    } 
} 

我不知道如何解決,我得到的是上面的行的錯誤,我不知道要放什麼東西在「其他」的時候mySensors不再等於null。如果我解決了上述錯誤,我可能會更好地瞭解新傳感器的添加過程。預先感謝您提供的任何幫助!

回答

1

mySensors = s應該mySensors.SensEl= s

因爲mySensors是SensorBlock型的,而s是sensor

void SensorNode::addSensorToNode(sensor *s) { 
    if(mySensors == NULL) //mySensors is first initialized to NULL 
    { 
        mySensors = new SensorBlock(); 
     mySensors->SensEl = s; //I get an error on this line. 
     mySensors->LLelement = NULL; 
    } 
    else { 

     SensorBlock newBlock = new SensorBlock(); 
     newBlock->SensEl = s; 
     newBlock->LLelement = NULL; 
     mySensors->LLelement = newBlock; 

    } 
} 
+0

*嫌頭*新手的錯誤,我一定是累了,謝謝你。我認爲它實際上需要mySensors-> SensEl = s;但相同的基本想法。感謝您向我指出 – Acoustic77

+0

當它不是鏈接列表的最開始時,我會在其他地方放置什麼? – Acoustic77

+0

@Acoustic,編輯了答案,但我認爲你可以通過統一傳感器類和傳感器塊來簡化數據結構 –

相關問題