Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;对多个类使用相同的ADT结构_C++_List_Adt - Fatal编程技术网

C++ C++;对多个类使用相同的ADT结构

C++ C++;对多个类使用相同的ADT结构,c++,list,adt,C++,List,Adt,例如,我必须使用列表ADT创建一个简单的医院队列系统,其中包含多个类。所以我的问题是typedef。既然类型def只能有一个数据类型,我该如何执行此操作 #include <string> #include "Patients.h" #include "Records.h" #include "Services.h" const int MAX_SIZE = 10000; typedef Patients ItemType; typedef Records Item

例如,我必须使用列表ADT创建一个简单的医院队列系统,其中包含多个类。所以我的问题是typedef。既然类型def只能有一个数据类型,我该如何执行此操作

  #include <string>
  #include "Patients.h"
  #include "Records.h"
  #include "Services.h"

const int MAX_SIZE = 10000;
typedef Patients ItemType;
typedef Records ItemType;         //Error Here
typedef Services ItemType;        //Error Here

class List
{
  private:
    ItemType items[MAX_SIZE];
    int      size;

  public:

List::List();

void List::display();

void List::replace(int index, ItemType item);

bool List::add(ItemType newItem);

bool List::add(int index, ItemType newItem);

void List::remove(int index);

ItemType List::get(int index); 

bool List::isEmpty(); 

int List::getLength();

};




#include <iostream>
#include "List.h"  // header file
using namespace std;
// constructor
List::List()
{
    size = 0;
}  

// add a new item to the back of the list (append)
bool List::add(ItemType newItem)
{
   bool success = size < MAX_SIZE;
   if (success)
   {  
      items[size] = newItem; // add to the end of the list
      size++;                // increase the size of the list by one
   }  
   return success;
}  

// add a new item at a specified position in the list (insert)
bool List::add(int index, ItemType newItem)
{
   bool success = (index >= 1) && (index <= size + 1) && (size < MAX_SIZE);
   if (success)
   {  
      for (int pos = size; pos >= index; pos--)
         items[pos] = items[pos-1];

      items[index-1] = newItem;
      size++;  // increase the size of the list by one
   }  
   return success;
}  

 // remove an item at a specified position in the list
void List::remove(int index)
{
   bool success = (index >= 1) && (index <= size);
   if (success)
   { 
      for (int fromPosition = index + 1; fromPosition <= size; fromPosition++)
         items[fromPosition - 2] = items[fromPosition - 1];

      size--; 
   }  

}  

// get an item at a specified position of the list (retrieve)
ItemType List::get(int index)
{
   ItemType dataItem;// = 0;
   bool success = (index >= 1) && (index <= size);
   if (success)
      dataItem = items[index - 1];

   return dataItem;
}  

// check if the list is empty
bool List::isEmpty()
{
   return size == 0;
}  

// check the size of the list
int List::getLength()
{
   return size;
}  


void List::replace(int index, ItemType item)
{
bool success = index >= 1 && index <= getLength();
if (success)
    items[index] = item;
}
#包括
#包括“Patients.h”
#包括“Records.h”
#包括“Services.h”
const int MAX_SIZE=10000;
项目类型;
typedef记录ItemType//这里出错
typedef服务项目类型//这里出错
班级名单
{
私人:
ItemType项目[最大大小];
整数大小;
公众:
List::List();
void List::display();
作废列表::替换(int索引,ItemType项);
bool List::add(ItemType newItem);
bool List::add(int索引,ItemType newItem);
作废列表::删除(int索引);
ItemType列表::get(int索引);
bool List::isEmpty();
int List::getLength();
};
#包括
#包括“List.h”//头文件
使用名称空间std;
//建造师
列表::列表()
{
尺寸=0;
}  
//将新项目添加到列表后面(追加)
bool List::add(ItemType newItem)
{
bool success=尺寸<最大尺寸;
如果(成功)
{  
items[size]=newItem;//添加到列表末尾
size++;//将列表的大小增加一
}  
回归成功;
}  
//在列表中的指定位置添加新项(插入)
bool List::add(int索引,ItemType newItem)
{
布尔成功=(索引>=1)和&(索引=索引;位置--)
项目[pos]=项目[pos-1];
项目[索引-1]=新项目;
size++;//将列表的大小增加一
}  
回归成功;
}  
//删除列表中指定位置的项目
无效列表::删除(int索引)
{

bool success=(index>=1)和&(index我建议您将
List
从类更改为类模板。有关更多想法,请参阅标准库中的
std::List
工作原理

因此,您可能有:

template<class ItemType>
class List
{
  private:
    ItemType items[MAX_SIZE];
    int      size;
  public:
    ItemType List::get(int index); 
    ...
};
模板
班级名单
{
私人:
ItemType项目[最大大小];
整数大小;
公众:
ItemType列表::get(int索引);
...
};
然后,您可以在声明列表时指定列表数据的类型:

List<Patients> allThePeople;
List<Records>  allThePapers;
List<Services> allTheWork;
列出所有人;
列出所有的纸张;
列出所有的工作;


当然,如果您创建
列表
代码是出于除类分配以外的任何原因,那么您应该真正使用
std::List

我建议您将
List
从类更改为类模板。有关更多想法,请参阅标准库中的
std::List
工作原理

因此,您可能有:

template<class ItemType>
class List
{
  private:
    ItemType items[MAX_SIZE];
    int      size;
  public:
    ItemType List::get(int index); 
    ...
};
模板
班级名单
{
私人:
ItemType项目[最大大小];
整数大小;
公众:
ItemType列表::get(int索引);
...
};
然后,您可以在声明列表时指定列表数据的类型:

List<Patients> allThePeople;
List<Records>  allThePapers;
List<Services> allTheWork;
列出所有人;
列出所有的纸张;
列出所有的工作;


当然,如果您创建
列表
代码是出于除课堂作业以外的任何原因,您应该真正使用
std::List

您应该使用模板:

#include <list>

typedef std::list<Patient> Patients;
typedef std::list<Record> Records;
typedef std::list<Service> Services;
#包括
typedef std::列出患者;
typedef std::列出记录;
typedef std::列表服务;

您应该使用模板:

#include <list>

typedef std::list<Patient> Patients;
typedef std::list<Record> Records;
typedef std::list<Service> Services;
#包括
typedef std::列出患者;
typedef std::列出记录;
typedef std::列表服务;

欢迎使用堆栈溢出!您是将
患者
记录
服务
存储在同一个列表中,还是将bw三个不同的列表?欢迎使用堆栈溢出!您是将
患者
记录
服务
存储在同一个列表中,还是将bw三个distinct lists?我必须对List.cpp文件所做的更改如何?您应该仔细阅读模板。简而言之,您需要将所有代码从List.cpp移到List.h。通过将所有代码移到List.h,Microsoft Visual Studio(C1001)会给我一个内部错误在我的第二个add function中,它适用于函数,但不适用于函数模板。正如我所说,第一步是阅读模板。我对List.cpp文件所做的更改如何?您应该阅读模板。简言之,您需要将所有代码从List.cpp移到List.h。通过将所有代码移到List.h,h将为我提供一个interMicrosoft Visual Studio(C1001)在我的第二个添加函数上出现nal错误,这对函数是正确的,但对函数模板不是正确的。正如我所说的,第1步是阅读模板。