C++;用类模板模拟FIFO的程序,退出队列时返回值322225477 我目前正在学习我在大学中第二次C++面向对象编程,所以我可能会有坏的编程实践和代码中的一般错误,所以,请指出,如果你看到任何。我总是乐于学习 我现在有一个C++模板的任务,在这里我必须创建一个程序,该类用一个不同的类来模拟FIFO(先进先出)队列,它是模板类型(队列 HumanQueue)。
队列.hC++;用类模板模拟FIFO的程序,退出队列时返回值322225477 我目前正在学习我在大学中第二次C++面向对象编程,所以我可能会有坏的编程实践和代码中的一般错误,所以,请指出,如果你看到任何。我总是乐于学习 我现在有一个C++模板的任务,在这里我必须创建一个程序,该类用一个不同的类来模拟FIFO(先进先出)队列,它是模板类型(队列 HumanQueue)。,c++,c++11,C++,C++11,队列.h #ifndef QUEUE_H #define QUEUE_H #include "human.h" template<class T> class Queue : public Human{ public: Queue(int = 5); ~Queue(); void enqueue(T); T dequeue(); void PrintQueue(); private:
#ifndef QUEUE_H
#define QUEUE_H
#include "human.h"
template<class T>
class Queue : public Human{
public:
Queue(int = 5);
~Queue();
void enqueue(T);
T dequeue();
void PrintQueue();
private:
T* array;
int size, index;
};
#endif
\ifndef队列
#定义队列
#包括“human.h”
模板
类队列:公共人员{
公众:
队列(int=5);
~Queue();
无效排队(T);
T退出队列();
void PrintQueue();
私人:
T*阵列;
int大小,索引;
};
#恩迪夫
队列.cpp
#include <iostream>
#include "queue.h"
using namespace std;
template<class T>
Queue<T>::Queue(int s){
array = new T[s];
size = s;
index = 0;
}
template<class T>
Queue<T>::~Queue(){
delete [] array;
}
// Add object to end of array
template<class T>
void Queue<T>::enqueue(T obj){
if(index == size){
cout << "Rinda ir pilna, nevar pievienot elementu!" << endl; // Array full, can't add any more objects
return;}
else{
array[index] = obj;
index++;}
}
// Remove object from start of array and shift the whole array by 1 position
template<class T>
T Queue<T>::dequeue(){
for(int i = 0; i < size; i++){
array[i] = array[i + 1];
}
index--;
}
template<class T>
void Queue<T>::PrintQueue(){
for(int i = 0; i < index; i++){
cout << i + 1 << ". ";
array[i].PrintHuman();
}
}
#include <iostream>
#include "human.h"
#include "queue.h"
#include "queue.cpp"
using namespace std;
int main(){
Queue<Human> HumanQueue(3);
Human a("Janis", 1.86, 76);
Human b("Peteris", 1.76, 69);
Human c("Arturs", 1.79, 75);
Human d("Aleksis", 1.81, 78);
cout << "Elementu rinda" << endl; // Element queue
HumanQueue.enqueue(a);
HumanQueue.enqueue(b);
HumanQueue.PrintQueue();
cout << "\n//Pievienojam elementu rindai//" << endl; // Add element to queue
HumanQueue.enqueue(c);
HumanQueue.PrintQueue();
cout << "\n//Meginam pievienot vel 1 elementu rindai//" << endl; // Trying to add one more element to queue, should return, that queue is full
HumanQueue.enqueue(d);
HumanQueue.PrintQueue();
cout << "\n//Iznemam 2 elementus no rindas//" << endl; // Dequeue 2 elements from queue
HumanQueue.dequeue();
HumanQueue.dequeue();
HumanQueue.PrintQueue();
system("pause");
return 0;
}
#包括
#包括“queue.h”
使用名称空间std;
模板
队列::队列(int s){
数组=新的T[s];
尺寸=s;
指数=0;
}
模板
队列::~Queue(){
删除[]数组;
}
//将对象添加到数组末尾
模板
无效队列::排队(T obj){
如果(索引==大小){
cout您的出列
函数不返回值
应该是这样的:
template<class T>
T Queue<T>::dequeue(){
if (index == 0) {
throw std::logic_error("queue is empty");
}
T value = array[0];
for(int i = 0; i < size - 1; i++){
array[i] = array[i + 1];
}
index--;
return value;
}
模板
T Queue::dequeue(){
如果(索引==0){
抛出std::logic_错误(“队列为空”);
}
T值=数组[0];
对于(int i=0;i
这个异常只是调用dequeue
时处理空队列的一个例子。为什么queue
继承自Human
?在dequeue
中:i
应该是i
,否则您将在最后一次迭代中使用i+1
访问出界,并且deque should为空队列做点什么,否则index
变为负值,hillarity随之发生。如果这对int
s有效,但不反对,则您的问题可能出现在Human
s/size/capacity/
和s/index/size/
的复制构造函数中。要使用更合适的命名,请注意,使用合适的标志,compiler可能会警告此错误。