我是 C++ 的新手,对模板的经验有限。目前,我正在尝试根据提供的示例实现并发队列 here .我在编译它时遇到问题,并不断收到错误消息,指出“ISO C++ 禁止声明没有类型的‘队列’”,即使我将代码简化为以下简单示例:
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
}
我对此有点困惑,因为我已经为队列提供了“数据”类型。有人可以帮我指出我做错了什么吗?
最佳答案
你有没有包括<queue>
?听起来编译器不知道 std::queue
是一个模板。因此它认为你在第三行定义它,但它无法弄清楚 queue
的类型是什么。会是。
关于c++ - 如何正确使用 std::queue 作为模板类中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666199/