2016-03-19 9 views
-1

私はC++ STLを使用してコードを作成しています。そして、私は "キュー"を使用したいと思います。 だから、私は以下のようなコードを書いた。 しかし、「キューはテンプレートではありません」というエラーが発生しました。 ご覧のとおり、「Common.h」ファイルにキュー(iostream、queue)に関連するヘッダを書き、「DataQueue.h」ファイルに「Common.h」をインクルードしました。しかし、VS2013 IDEツールは、キューがテンプレートではないので、 'queue m_deQueue'はエラーであると言っています。なぜこのエラーが発生したのかわかりません。どんな助けもありがとう!標準ライブラリ、我々は見つけるのMSの実装におけるから<queue>ヘッダの先頭に座っキューはテンプレートではありません

//[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

私は[読んで** this **]と思う(https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered) -bad-practice)を続行することをお勧めします。そして、fyi、 '_QUEUE_'と' _COMMON_'はどちらも実装のための予約識別子であり、あなたのヘッダfencepost idsとして使うべきではありません。 – WhozCraig

答えて

4

...独自のヘッダーfencepostため、その識別子の使用状況を意味

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

が排除されますMSヘッダ本体が引き込まれないようにします。したがって、std::queueはありません。別のIDを使用してください。このIDは、実装用に予約されているマクロ定数(このような)の使用規則に違反しないものを使用することをお勧めします。

子供たちは、実装のために予約された識別子を使用しない理由があります。詳細については、この質問をお読みください:"What are the rules about using an underscore in a C++ identifier?"

+0

ありがとうございます!私はそれを固定し、それは動作します! –