0

I've been creating codes using C++ STL. And I want to use "queue". So, I wrote the codes like below. But, I encountered "queue is not a template" error. As you can see, I wrote headers related with queue (iostream, queue) in "Common.h" file and wrote include "Common.h" in "DataQueue.h" file. But, VS2013 IDE tool said that 'queue m_deQueue' is error because queue is not a template. I don't know why.. this error occured. Any help is appreciated!

//[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;
}
Seung-hee Han
  • 11
  • 1
  • 2
  • I think [reading **this**](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) before continuing would be advisable. And fyi, `_QUEUE_` and `_COMMON_` are both reserved identifiers for the implementation, and shouldn't be used as your header fencepost ids. – WhozCraig Mar 19 '16 at 05:20

1 Answers1

5

Sitting at the top of the <queue> header from the MS implementaiton of the standard library, we find...

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

Which means your usage of that identifier for your own header fencepost is precluding the MS header body from being pulled in. Thus, no std::queue for you. Use a different id, preferably something that doesn't violate usage rules for macro constants reserved for the implementation (like this one).

And that, kids, is why we don't use identifiers reserved for implementation usage. For more information, read this question: "What are the rules about using an underscore in a C++ identifier?"

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141