简体   繁体   中英

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?

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.

The error here means that the constructor is private and therefore can't be called. Further, since you don't call it explicitly, the default constructor is called as part of CMsqQueueMngr. Since it is (probably) intentionally private, you need to call the proper constructor or something similar, but you are trying to use the class in a way it wasn't intended to be used. The solution is to study the instructions. From those, it should be clear what to do, ie pass the proper arguments via the initializer list:

CMsqQueueMngr::CMsqQueueMngr(char *queueName):
    mq(create_only, queueName, 100, sizeof (int))
{}

Notes:

  • Since the code now only receives a single argument to the ctor, you should make it explicit .
  • You should not use a pointer to (non- const ) char as name, use a std::string .
  • In general, study the idea of "const correctness".
  • You must provide the queueName to the constructor. If you don't want that, you have to dynamically allocate the queue.
  • As alternative, you might create a local instance of the message queue whenever you need it, but that doesn't sound like a good idea. You need to decide that depending on your design.
  • It seems your messages are uint8_t s, so why is your message size configured as size of an int ?
  • Avoid "magic numbers" like 100.
  • int mode is also a bad choice, use an enumeration instead if that is what this is supposed to mean.
  • Do not catch exceptions only to return -1 (another magic number). Errorcodes are bad, just let the exception fly unless you really handle it.
  • Next time, indent you code properly. After all, you want people to read it, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM