Search code examples
c++boostcompiler-errorsmessage-queueboost-interprocess

Again on error C2248


the error C2248 is not new on stackoverflow. Unfortunately I'm a beginner in using Boost library and I'm not able to fix the error in my code:

// .h file

using namespace boost::interprocess;
using namespace std;

class CMsqQueueMngr {

public:
    // constructors & destructors
    CMsqQueueMngr();
    ~CMsqQueueMngr();

    int Open(char *queueName, int mode);
    int Close();
    int Read(uint8_t *data, int count);
    int Write(uint8_t *data, int count, int priority);

    boost::interprocess::message_queue mq;

private:
    std::string mqName;

};

// .cpp file

CMsqQueueMngr::CMsqQueueMngr()
{} **<=== ERROR C2248** 

CMsqQueueMngr::~CMsqQueueMngr()
{}

int CMsqQueueMngr::Open(char *queueName, int mode)
{
    try{
        //Erase previous message queue
        message_queue::remove(queueName);

        mqName.assign(queueName);

        //Create a message_queue.
        mq
            (create_only               //only create
            , queueName                 //name
            , 100                       //max message number
            , sizeof(int)               //max message size
            );  **<=== ERROR C2064 **


        //Send 100 numbers
        for (uint8_t i = 0; i < 100; ++i){
            mq.send(&i, sizeof(i), 0);
        }
    }
    catch (interprocess_exception &ex){
        std::cout << ex.what() << std::endl;
        return -1;
    }

    return 0;

}

The compiler errors:

error C2248: 'boost::interprocess::message_queue_t>::message_queue_t': cannot access private member declared in class'boost::interprocess::message_queue_t>

error C2064: term does not evaluate to a function taking 4 arguments

How can I make the variable mq accessible?


Solution

  • You have to create the message queue object mq in the constructor of you containing class,

    CMsqQueueMngr::CMsqQueueMngr(): 
        mq(create_only, "my_first_queue_name", 100, sizeof(int)) 
    {
    }
    

    and you have to do it using an initalizer list, because there is no accessible default constructor for message queue objects (like mq). This is the meaning of your compiler's message C2248 after the closing brace of your constructor.

    BTW: Members can never be initialized within normal methods, this is what the compiler found to be wrong (thus C2064) in your Open method. There are some other errors (or misunderstandings, or open ends) in it whereas the call to mq.send will work as expected (at least once).


    [Update]

    Alternatively, you may access the boost's message queue with a variable on the stack:

    /// abbreviate name boost::interprocess::message_queue
    using boost::interprocess::message_queue;
    
    class CMsqQueueMngr {
    
    public:
        CMsqQueueMngr(const std::string& queue_name);
    
        void WriteInt(int data);
    
        // (some methods omitted)
    
    private:
    
        /// name of the queue
        std::string mqName;
    
    };
    
    CMsqQueueMngr::CMsqQueueMngr(const std::string& name):
        mqName(name)
    {
    }
    
    void CMsqQueueMngr::WriteInt(const int data)
    {
        // create a queue for max 100 values at first call, open if existing
        message_queue mq(open_or_create, mqName.c_str(), 100, sizeof (data));
        mq.send(&data, sizeof(data), 0);
    }
    

    ...I did not try this, but if not possible, the static remove method wouldn't make much sense.