//头文件 MyQueue.h #ifndef MYQUEUE_H #define MYQUEUE_H #include <iostream> #include <string> using namespace std; template <typename T> class MyQueue { public: MyQueue::MyQueue(int queueCapacity) { m_iQueueCapacity = queueCapacity; m_iHead = 0; m_iTail = 0; m_iQueueLen = 0; m_pQueue = new T[m_iQueueCapacity]; } MyQueue::~MyQueue() { delete m_pQueue; m_pQueue = NULL; } void MyQueue::ClearQueue() { m_iHead = 0; m_iTail = 0; m_iQueueLen = 0; } bool MyQueue::QueueEmpty() const { if (m_iQueueLen == 0) { return true; } else { return false; } //return m_iQueueLen == 0 ? true:false; } bool MyQueue::QueueFull() const { if (m_iQueueLen == m_iQueueCapacity) { return true; } else { return false; } //return m_iQueueLen==m_iQueueCapacity? true:false; } T MyQueue::QueueLength() const { return m_iQueueLen; } bool MyQueue::EnQueue(T element) { if (QueueFull() == true) { return false; } else { m_pQueue[m_iTail] = element; m_iTail++; m_iTail = m_iTail%m_iQueueCapacity; m_iQueueLen++; return true; } } bool MyQueue::DeQueue(T & element) { if (QueueEmpty() == true) { return false; } else { element = m_pQueue[m_iHead]; m_iHead++; m_iHead = m_iHead%m_iQueueCapacity; m_iQueueLen--; return true; } } void MyQueue::QueueTraverse() { for (int i = m_iHead;i < m_iHead + m_iQueueLen;i++) { cout << m_pQueue[i%m_iQueueCapacity] << endl; } } private: T *m_pQueue; //队列数组指针 int m_iQueueLen; //队列元素个数 int m_iQueueCapacity; //队列数组容量 int m_iHead; //队列头 int m_iTail; //队列尾 }; #endif
//源文件 demo.cpp #include "MyQueue.h" int main() { MyQueue<string> *p=new MyQueue<string>(4); p->EnQueue("A"); p->EnQueue("1"); p->EnQueue("6"); p->EnQueue("8"); p->QueueTraverse(); string e="0"; p->DeQueue(e); cout << e << endl; p->DeQueue(e); cout << e << endl; p->QueueTraverse(); p->ClearQueue(); p->QueueTraverse(); p->EnQueue("c"); p->EnQueue("f"); p->QueueTraverse(); delete p; p=NULL; }
错误提示如图,求大神指点下哪里错了
改好了
类内定义函数不用加作用域限定符
以下代码
//源文件 demo.cpp #include "MyQueue.h" #include <iostream> using namespace std; int main() { MyQueue<string> *p=new MyQueue<string>(4); p->EnQueue("A"); p->EnQueue("1"); p->EnQueue("6"); p->EnQueue("8"); p->QueueTraverse(); string e="0"; p->DeQueue(e); cout << e << endl; p->DeQueue(e); cout << e << endl; p->QueueTraverse(); p->ClearQueue(); p->QueueTraverse(); p->EnQueue("c"); p->EnQueue("f"); p->QueueTraverse(); delete p; p=NULL; return 0; }
//头文件 MyQueue.h #ifndef MYQUEUE_H #define MYQUEUE_H #include <iostream> #include <string> using namespace std; template <typename T> class MyQueue { public: MyQueue(int queueCapacity) { m_iQueueCapacity = queueCapacity; m_iHead = 0; m_iTail = 0; m_iQueueLen = 0; m_pQueue = new T[m_iQueueCapacity]; } ~MyQueue() { delete m_pQueue; m_pQueue = NULL; } void ClearQueue() { m_iHead = 0; m_iTail = 0; m_iQueueLen = 0; } bool QueueEmpty() const { if (m_iQueueLen == 0) { return true; } else { return false; } //return m_iQueueLen == 0 ? true:false; } bool QueueFull() const { if (m_iQueueLen == m_iQueueCapacity) { return true; } else { return false; } //return m_iQueueLen==m_iQueueCapacity? true:false; } T QueueLength() const { return m_iQueueLen; } bool EnQueue(T element) { if (QueueFull() == true) { 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() == true) { 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++) { cout << m_pQueue[i%m_iQueueCapacity] << endl; } } private: T *m_pQueue; //队列数组指针 int m_iQueueLen; //队列元素个数 int m_iQueueCapacity; //队列数组容量 int m_iHead; //队列头 int m_iTail; //队列尾 }; #endif
还有,demo.cpp可能还有点小问题,不过影响不大,程序能正常运行
再来张图