开发者

How do I correctly use a std::queue as a element in a template class?

开发者 https://www.devze.com 2023-01-15 01:15 出处:网络
I am reasonably new to C++ and have limited experience of templates. At the moment I am trying to implement a concurrent queue based on the example provided here. I am having problems compiling it and

I am reasonably new to C++ and have limited experience of templates. At the moment I am trying to implement a concurrent queue based on the example provided here. I am having problems compiling it and keep getting an error stating that "ISO C++ forbids declaration of ‘queue’ with no type" even after I stripped down the code to the following simple example:

template<typename Data> class concurrent_queue {
private:
    std::queue<Data> the_queue;

public:
    void push(Data const& data) {
        the_queue.push(data);
    }

    bool empty() const {
        return the_queue.empty();
    }

    void pop(Data& popped_value) {
        popped_value=the_queue.front();
        the_queue.pop();
    }
};

int main(int argc, char** argv) {
    concurrent_queue<std::string> Q;

    // Simple test code 开发者_运维百科will go here
}

I am a bit puzzled by this since I have provided the type 'Data' for the queue. Can someone please help me point out what I have done wrong?


Have you included <queue> ? It sounds like the compiler doesn't know that std::queue is a template. It therefore thinks you're defining it on the third line, but it can't figure out what the type of queue would be.


You are missing #include <queue> and #include<string> at the beginning of the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消