C++ 使用具有给定.h文件c+的结构类创建城市链接列表+; \ifndef通信网络 #定义通信网络 #包括 结构城市{ std::字符串cityName; std::字符串消息; 城市*下一个; City(){};//默认构造函数 城市(std::string initName,City*initNext,std::string initMessage) { cityName=initName; next=initNext; message=initMessage; } }; 班级交流网络 { 公众: 通信网络(); ~CommunicationNetwork(); void addCity(std::string,std::string); void buildNetwork(); void transmitsg(char*);//这就像一个字符串 void printNetwork(); 受保护的: 私人: 城市*负责人; 城市*尾巴; }; #endif//通信网络

C++ 使用具有给定.h文件c+的结构类创建城市链接列表+; \ifndef通信网络 #定义通信网络 #包括 结构城市{ std::字符串cityName; std::字符串消息; 城市*下一个; City(){};//默认构造函数 城市(std::string initName,City*initNext,std::string initMessage) { cityName=initName; next=initNext; message=initMessage; } }; 班级交流网络 { 公众: 通信网络(); ~CommunicationNetwork(); void addCity(std::string,std::string); void buildNetwork(); void transmitsg(char*);//这就像一个字符串 void printNetwork(); 受保护的: 私人: 城市*负责人; 城市*尾巴; }; #endif//通信网络,c++,struct,linked-list,header-files,C++,Struct,Linked List,Header Files,我只是想知道这个.h到底做了什么/设置了什么,以及如何在我的CommunicationsNetwork.cpp和main.cpp中继续构建给定城市的列表 注意:这段代码最终应该能够将城市添加到列表中,打印出链接列表中的城市并发送消息,但我目前只对尝试创建链接列表感兴趣。正如我看到的CommunicationsNetwork.h具有结构和类的声明,因此,在通信网络中.cpp必须是类通信网络的所有成员方法的定义,类似于: #包括“CommunicationNetwork.h” . . . // 其

我只是想知道这个.h到底做了什么/设置了什么,以及如何在我的CommunicationsNetwork.cpp和main.cpp中继续构建给定城市的列表


注意:这段代码最终应该能够将城市添加到列表中,打印出链接列表中的城市并发送消息,但我目前只对尝试创建链接列表感兴趣。

正如我看到的
CommunicationsNetwork.h
具有结构和类的声明,因此,在
通信网络中.cpp
必须是
类通信网络
的所有成员方法的定义,类似于:

#包括“CommunicationNetwork.h”
. . . //  其他一些#包括指令
通信网络::通信网络(){
. . . 
}
. . .
无效通信网络::打印网络()
{
. . .
}
要在
main.cpp
中使用
CommunicationNetwork
class和
City
struct,您需要:

  • 将h文件包含到
    main.cpp
    as
    #包含“CommunicationNetwork.h”
  • 使用
    main.cpp
    编译
    CommunicationsNetwork.cpp
    (即用一个二进制文件链接编译的文件)
  • 如果您没有
    CommunicationsNetwork.cpp
    并且您的任务是为类
    CommunicationsNetwork
    的方法编写定义,那么您必须从设计所有操作的算法开始(我的意思是,考虑如何构建网络,如何添加城市等)

    默认构造函数可以类似于:

    CommunicationNetwork::CommunicationNetwork()
    {
    head=NULL;
    tail=NULL;
    }
    
    析构函数(即,
    CommunicationNetwork::~CommunicationNetwork()
    )必须从列表中删除所有元素,并释放为元素存储分配的内存

    请记住,在将城市添加到网络时,检查
    的值(添加到空列表可能略有不同,因为在第一个元素之后,
    也是


    所以,开始写代码吧,祝你好运

    我猜你应该实现
    CommunicationNetwork
    中声明的功能。你在之前的作业中做得如何?如果你得到了这些,你也应该得到一些关于一切是什么以及它们应该做什么的描述。对于局外人来说,这是不可能猜到的。
    #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