我一直在使用C++ STL創建代碼。我想用「隊列」。 所以,我寫了下面的代碼。 但是,我遇到了「隊列不是模板」的錯誤。如您所見,我在「Common.h」文件中編寫了與隊列(iostream,隊列)相關的頭文件,並在「DataQueue.h」文件中寫入了「Common.h」。但是,VS2013 IDE工具說'隊列m_deQueue'是錯誤的,因爲隊列不是模板。我不知道爲什麼..這個錯誤發生。任何幫助表示讚賞!隊列不是模板
//[Common.h]
#ifndef _COMMON_
#define _COMMON_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
//thread related headers
#include <Windows.h>
#include <process.h>
//socket related headers
#include <winsock.h>
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
#endif
//[DataQueue.h]
#ifndef _QUEUE_
#define _QUEUE_
#include "SocketStruct.h"
#include "Common.h"
class CDataQueue{
private:
static CDataQueue* m_cQueue;
// deque <ST_MONITORING_RESULT> m_deQueue;
queue <ST_MONITORING_RESULT> m_deQueue;
CRITICAL_SECTION m_stCriticalSection;
CDataQueue();
~CDataQueue();
public:
static CDataQueue* getDataQueue(){
if (m_cQueue == NULL){
m_cQueue = new CDataQueue();
}
return m_cQueue;
}
deque <ST_MONITORING_RESULT> getQueue();
void pushDataToQueue(ST_MONITORING_RESULT data);
ST_MONITORING_RESULT popDataFromQueue();
};
#endif
//[DataQueue.cpp]
#include "DataQueue.h"
CDataQueue* CDataQueue::m_cQueue = NULL;
CDataQueue::CDataQueue(){
::InitializeCriticalSection(&m_stCriticalSection);
// m_mutex = PTHREAD_MUTEX_INITIALIZER;
}
CDataQueue::~CDataQueue(){
::DeleteCriticalSection(&m_stCriticalSection);
}
::deque <ST_MONITORING_RESULT> CDataQueue::getQueue(){
return m_deQueue;
}
void CDataQueue::pushDataToQueue(ST_MONITORING_RESULT data){
::EnterCriticalSection(&m_stCriticalSection);
m_deQueue.push_back(data);
::LeaveCriticalSection(&m_stCriticalSection);
}
ST_MONITORING_RESULT CDataQueue::popDataFromQueue(){
::EnterCriticalSection(&m_stCriticalSection);
ST_MONITORING_RESULT data = m_deQueue.front();
m_deQueue.pop_front();
::LeaveCriticalSection(&m_stCriticalSection);
return data;
}
我認爲[閱讀**此**](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered在實施之前)是可取的。而fyi,'_QUEUE_'和'_COMMON_'都是實現的保留標識符,不應該用作您的標題fencepos id。 – WhozCraig