C++队列中应该注意的一些问题

本文介绍了在C++中编写类的基本概念,并分享了几个关键点需要注意的地方,包括类的构造函数、模板使用、成员函数定义等。同时,提供了一些常见问题的解答,帮助初学者更好地理解并应用C++类。

第一次在C++中写类,新手,见笑

#include<iostream.h>
#include<iostream>
template<typename T>
class Queue
{
private:
int maxsize,front,rear;
T *q;
public:
Queue()
{
q=new T[maxsize];
rear=front=0;
}
~Queue()
{
delete []q;
}
bool AddQ(T item);
bool Delete(T &item);
bool QFull();
bool QEmpty();
int getsize();
};
template<typename T>
bool Queue<T> ::AddQ(T item)
{
rear=(++rear)%maxsize;
if(rear==front)
{
printf("The Queue is full");
if(!front)
{
rear=maxsize;
}
else
{
rear=rear-1;
}
return false;
}
else
{
q[rear]=item;
return true;
}
}
template<typename T>
bool Queue<T> ::Delete(T &item)
{
if(front==rear)
{
printf("The queue is empty!");
return false;
}
else
{
front=front+1;
front=front%maxsize;
item=q[front];
return true;
}
}
template<typename T>
int Queue<T> ::getsize()
{
return rear-front;
}

 

1、首先吧,类应该写到.h文件中

2、其次应该写与类名一样的构造函数

Queue()
{
q=new T[maxsize];
rear=front=0;
}

3、template<typename T> 

这样的话就可以不限制使用的数据类型了

4、class之后的{}应该加;

5、

bool AddQ(T item);
bool Delete(T &item);
bool QFull();
bool QEmpty();
int getsize();

在类外写这些函数时,每一个函数前应该加template<typename T> 

6、还有一个问题:

Delete(T &item)

Add(T item)

为什么一个取地址为什么另一个不取地址

试了一下,好像暂时两个都可以运行

 

转载于:https://www.cnblogs.com/yunerlalala/p/5482734.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值