2016-02-05 60 views
-1
#ifndef COMMUNICATIONNETWORK_H 
#define COMMUNICATIONNETWORK_H 
#include <iostream> 
    struct City{ 
    std::string cityName; 
    std::string message; 
    City *next; 

    City(){}; // default constructor 

    City(std::string initName, City *initNext, std::string initMessage) 
    { 
     cityName = initName; 
     next = initNext; 
     message = initMessage; 
    } 

}; 

class CommunicationNetwork 
{ 
    public: 
     CommunicationNetwork(); 
     ~CommunicationNetwork(); 
     void addCity(std::string, std::string); 
     void buildNetwork(); 
     void transmitMsg(char *); //this is like a string 
     void printNetwork(); 
    protected: 
    private: 
     City *head; 
     City *tail; 
}; 

#endif // COMMUNICATIONNETWORK_H 

我只是想知道創建城市的鏈表究竟該.H並/套了,我怎麼會在我CommunicationsNetwork.cpp進行以及我的main.cpp構建給定城市的列表。使用結構類給出.h文件中C++

注意:此代碼最終應該能夠將城市添加到列表中,打印鏈表中的城市並傳輸消息,但我目前只是試圖創建鏈接列表。

+1

我想你應該實現'CommunicationNetwork'中聲明的函數。 –

+0

你在早先的作業中做了些什麼? – Beta

+0

如果你給了這些,你應該也已經被描述了什麼是一切以及他們應該做什麼。對於局外人來說,這是不可能猜到的。 – molbdnilo

回答

0

當我看到CommunicationsNetwork.h對結構和類聲明,所以在CommunicationsNetwork.cpp必須爲class CommunicationNetwork所有成員方法定義是這樣的:

 #include "CommunicationNetwork.h" 
     . . . // some other #include directives 
     CommunicationNetwork::CommunicationNetwork(){ 
      . . . 
     } 
     . . . 
     void CommunicationNetwork::printNetwork() 
     { 
      . . . 
     } 

要使用CommunicationNetwork類和City結構在main.cpp你需要:

  1. 包括H-文件到main.cpp#include "CommunicationNetwork.h"
  2. 編制CommunicationsNetwork.cppmain.cpp(即在一個二進制鏈接編譯的文件)

如果你還沒有CommunicationsNetwork.cpp,你的任務是寫一個類的方法定義CommunicationsNetwork你必須從所有的動作(我的意思是設計算法開始,考慮如何建立網絡,如何添加城市等)。

默認的構造可以像:

CommunicationNetwork::CommunicationNetwork() 
{ 
    head = NULL; 
    tail = NULL; 
} 

析構函數(即CommunicationNetwork::~CommunicationNetwork())必須刪除從你的清單,分配給元素存儲的可用內存的所有元素。

記住檢查的head和增加城市時tail價值網絡(增加空單可略有不同,因爲後第一個元素做了一個head也是tail)。

那麼,開始寫代碼和祝你好運!