2016-03-19 56 views
-1

我一直在使用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; 
} 
+0

我認爲[閱讀**此**](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered在實施之前)是可取的。而fyi,'_QUEUE_'和'_COMMON_'都是實現的保留標識符,不應該用作您的標題fencepos id。 – WhozCraig

回答

4

在標準庫中,我們發現的MS implementaiton的<queue>頭頂部坐在...

// queue standard header 
#pragma once 
#ifndef _QUEUE_ 
#define _QUEUE_ 

這意味着你該標識符爲自己的首部柵欄柱的使用被排除MS標題正文被拉入。因此,沒有std::queue給你。使用不同的ID,最好是不違反用於實現的宏常量的使用規則(比如這個)。

而且,孩子們,這就是爲什麼我們不使用標識符保留實施使用。欲瞭解更多信息,請閱讀以下問題:"What are the rules about using an underscore in a C++ identifier?"

+0

非常感謝!我解決了它,它的工作原理! –