亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

用模板實現的問題

使用類模板的話,遍歷的輸出應該怎么寫,因為輸出int和類的方法不一樣 請大神告知

正在回答

4 回答

不好意思,第85行的QueueEmpty()函數調用多了一對括號。

0 回復 有任何疑惑可以回復我~
void?MyQueue::QueueTraverse(){
????for(int?i=m_iHead;?i?<?m_iHead?+?m_iQueueLen;?i++){
????????m_pQueue[m_iHead].printInfo();
????}
}


//其中printInfo()是自定義類型實現的函數,例如
void?Customer::printInfo(){
????cout?<<?”姓名:?"?<<?m_strName?<<?endl;
????cout?<<?"年齡:?"?<<?m_iAge?<<?endl;
????cout?<<?endl;
}

再貼上一個我實現的吧,直接復制下面的代碼可運行。

#include?<iostream>
#include?<string>

using?namespace?std;


class?Customer{
public:
	Customer(){
		//需要默認構造函數
	}
	
	Customer(string?name,?int?age){
		m_strName?=?name;
		m_iAge?=?age;
	}

	void?printInfo()?const{
		cout?<<?"姓名:?"?<<?m_strName?<<?endl;
		cout?<<?"年齡:?"?<<?m_iAge?<<?endl;
		cout?<<?endl;
	}
	
private:
	string?m_strName;
	int?m_iAge;
};




template?<class?T>
class?MyQueue{
public:
	MyQueue(int?queueCapacity){
		m_iQueueCapacity?=?queueCapacity;
		m_iQueueLen?=?0;
		m_iHead?=?0;
		m_iTail?=?0;
		m_pQueue?=?new?T[queueCapacity];
	}
	
	~MyQueue(){
		delete[]?m_pQueue;
	}
	
	void?QueueClear(){
		m_iQueueLen?=?0;
		m_iHead?=?0;
		m_iTail?=?0;
	}
	
	bool?QueueEmpty()?const{
		if(m_iQueueLen==0){
			return?true;
		}
		else{
			return?false;
		}
	}
	
	bool?QueueFull()?const{
		if(m_iQueueLen==m_iQueueCapacity){
			return?true;
		}
		else{
			return?false;
		}
	}
	
	bool?EnQueue(T?element){
		if(QueueFull()){
			return?false;
		}
		else{
			m_pQueue[m_iTail]?=?element;
			m_iTail?++;
			m_iTail?=?m_iTail?%?m_iQueueCapacity;
			m_iQueueLen?++;
			return?true;
		}
	}
	
	bool?DeQueue(T?&element){
		if(QueueEmpty()()){
			return?false;
		}
		else{
			element?=?m_pQueue[m_iHead];
			m_iHead?++;
			m_iHead?=?m_iHead?%?m_iQueueCapacity;
			m_iQueueLen?--;
			return?true;
		}
	}
	
	void?QueueTraverse(){
		for(int?i?=?m_iHead;?i?<?m_iHead?+?m_iQueueLen;?i++){
			m_pQueue[i%m_iQueueCapacity].printInfo();
		}
	}
	
private:
	T*?m_pQueue;
	int?m_iHead;
	int?m_iTail;
	int?m_iQueueLen;
	int?m_iQueueCapacity;
};





int?main(int?argc,?char?*argv[])?{
	MyQueue<Customer>*?p?=?new?MyQueue<Customer>(4);
	p->EnQueue(Customer("imooc",?20));
	p->QueueTraverse();
}


0 回復 有任何疑惑可以回復我~

template <class T>

void MyQueue::QueueTraverse()

{

// 里面T即你需要的類型

}


0 回復 有任何疑惑可以回復我~

template <class T>

void MyQueue::QueueTraverse(){

?//some code

}

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

用模板實現的問題

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號