MyQueue.cpp的構(gòu)造函數(shù)第二行代碼報錯:[Error] no matching function for call to 'Customer::Customer()',請問怎么修改
//MyQueue.h
#ifndef MYQUEUE_H
#define MYQUEUE_H
#include "Customer.h"
class MyQueue
{
public:
?? ?MyQueue(int queueCapacity);//創(chuàng)建隊列
?? ?virtual ~MyQueue();//銷毀隊列
?? ?void ClearQueue();//清空隊列
?? ?bool QueueEmpty() const;//判空隊列
?? ?bool QueueFull() const;//判滿隊列
?? ?int QueueLength() const;//隊列長度
?? ?bool EnQueue(Customer element);//新元素入隊
?? ?bool DeQueue(Customer &element);//首元素出隊
?? ?void QueueTraverse();//遍歷隊列
private://成員變量
?? ?Customer *m_pQueue;//隊列數(shù)組指針(要注意插入元素的類型必須和數(shù)組的類型系統(tǒng),這里都應(yīng)該是int型)
?? ?int m_iQueueLen;//隊列元素個數(shù)
?? ?int m_iQueueCapacity;//隊列數(shù)組容量
?? ?int m_iHead;
?? ?int m_iTail;
};
#endif
//MyQueue.cpp
#include "MyQueue.h"
#include <iostream> //為了使用cin,cout
using namespace std;
MyQueue::MyQueue(int queueCapacity)
{
?? ?m_iQueueCapacity =?? ?queueCapacity;
?? ?m_pQueue = new Customer[m_iQueueCapacity];//從堆中為數(shù)組分配內(nèi)存
?? ?ClearQueue();
}
MyQueue::~MyQueue()
{
?? ?delete []m_pQueue;//銷毀數(shù)組的內(nèi)存
?? ?m_pQueue = NULL;//指針指向空指針
}
void MyQueue::ClearQueue()//清空元素,內(nèi)存還在
{
?? ?m_iHead = 0;
?? ?m_iTail = 0;
?? ?m_iQueueLen = 0;
}
bool MyQueue::QueueEmpty() const
{
?? ?if(m_iQueueLen == 0)
?? ?{
?? ??? ?return true;
?? ?}
?? ?else
?? ?{
?? ??? ?return false;
?? ?}
}
bool MyQueue::QueueFull() const
{
?? ?if(m_iQueueLen == m_iQueueCapacity)
?? ?{
?? ??? ?return true;
?? ?}
?? ?else
?? ??? ?return false;
}
int MyQueue::QueueLength() const
{
?? ?return m_iQueueLen;
}
/*為隊列插入元素
?? ?1.判滿
?? ?2.沒滿,則插入元素
?? ?3.滿,則無法插入,返回false
?? ?注:隊尾指針或下標(biāo)指向的是要插入的位置
?? ?注:下標(biāo)越界問題,用取余解決
?? ?*/
bool MyQueue::EnQueue(Customer element)
{
?? ?if(QueueFull())
?? ?{
?? ??? ?return false;
?? ?}
?? ?else
?? ?{
?? ??? ?m_pQueue[m_iTail] = element;
?? ??? ?m_iTail++;
?? ??? ?m_iTail = m_iTail % m_iQueueCapacity;
?? ??? ?m_iQueueLen++;
?? ??? ?return true; ?
?? ?}
}
/*元素出隊:
?? ?1.判空隊列
?? ?2.隊列為空,返回false,無法出隊
?? ?3.隊列不空,首元素出隊
?? ?注:下標(biāo)越界問題,用取余解決
?? ?*/
bool MyQueue::DeQueue(Customer &element)
{
?? ?if(QueueEmpty())
?? ?{
?? ??? ?return false;
?? ?}
?? ?else
?? ?{
?? ??? ?element = m_pQueue[m_iHead];
?? ??? ?m_iHead++;
?? ??? ?m_iHead = m_iHead % m_iQueueCapacity;
?? ??? ?m_iQueueLen--;
?? ??? ?return true;
?? ?}
}
????????????????????????????????????? ?
/*遍歷元素
?? ?把隊列中的元素打印出來
?? ?遍歷中一般都有循環(huán)操作
?? ?注:m_iQueueLen+m_iHead為防止:頭指針不從0開始,? i < m_iQueueLen會導(dǎo)致數(shù)組的后邊幾個元素遍歷不到
?? ?*/
void MyQueue::QueueTraverse()
{
?? ?for(int i=m_iHead ; i < m_iQueueLen+m_iHead ; i++)
?? ?{
?? ??? ?m_pQueue[i%m_iQueueCapacity].printIofo();
?? ?}????????????????????????????????????????????????????????????????????? ?
}
//Customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
using namespace std;
class Customer
{
public:
?? ?Customer(string name,int age);
?? ?void printInfo() const;
private:
?? ?string m_strName;
?? ?int m_iAge;
};
#endif
//Customer.cpp
#include "iostream"
#include "Customer.h"
using namespace std;
Customer::Customer(string name = "",int age =10)
{
?? ?m_strName = name;
?? ?m_iAge = age;
}
void Customer::printInfo() const
{
?? ?cout << "姓名:" << m_strName << endl;
?? ?cout << "年齡:" << m_iAge << endl;
}
//demo.cpp
#include <iostream>
#include <stdlib.h>
#include "MyQueue.h"
#include "Customer.h"
using namespace std;
//環(huán)形隊列檢測
int main(void)
{
??? MyQueue *p = new MyQueue(4);
?? ?Customer c1("zhang",20);
?? ?Customer c2("li",21);
?? ?Customer c3("wang",22);
?? ?
?? ?p->EnQueue(c1);
?? ?p->EnQueue(c2);
?? ?p->EnQueue(c3);
?? ?
?? ?p->QueueTraverse();
?? ?system("pause");
?? ?return 0;
}
2018-04-09
Customer(string name,int age); 將此行修改為?Customer(string name = "",int age = 0);?