user2896235 user2896235 - 3 months ago 16
C++ Question

Issue with Boost Library Queues

I am trying to use queues from boost library.

When my queue definitions are as below, the compilation passes and everything works as expected.

#include <boost/lockfree/queue.hpp>
#include <string>
#include <iostream>

boost::lockfree::queue<int> queue1(128);
boost::lockfree::queue<int> queue2(128);


However when I change my code as shown below (i.e wrap the queue inside a structure), the compilation fails with the below errors.

#include <boost/lockfree/queue.hpp>
#include <string>
#include <iostream>

typedef struct stack {
int top;
boost::lockfree::queue<int> queue1(128);
boost::lockfree::queue<int> queue2(128);
} stack;


~/prgms$ g++ two_queue_to_stack.cpp
two_queue_to_stack.cpp:9:38: error: expected identifier before numeric constant
boost::lockfree::queue<int> queue1(128);
^
two_queue_to_stack.cpp:9:38: error: expected ',' or '...' before numeric constant
two_queue_to_stack.cpp:10:38: error: expected identifier before numeric constant
boost::lockfree::queue<int> queue2(128);
^
two_queue_to_stack.cpp:10:38: error: expected ',' or '...' before numeric constant


Whats wrong with the above definitions ? Am i missing some thing basic here ?

Answer

Use member initializer lists:

struct stack { 
  int top;
  boost::lockfree::queue<int> queue1, queue2;

  // initialize the member objects, queue1, queue2, during construction of stack
  stack() : queue1(128), queue2(128) {
  }