再次出现错误C2248

时间:2015-02-02 11:04:03

标签: c++ boost compiler-errors message-queue boost-interprocess

错误C2248在stackoverflow上不是新的。不幸的是我是使用Boost库的初学者,我无法在我的代码中修复错误:

// .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;

}

编译器错误:

错误C2248:'boost :: interprocess :: message_queue_t&gt; :: message_queue_t':无法访问class'boost :: interprocess :: message_queue_t&gt;

中声明的私有成员

错误C2064:术语不评估为采用4个参数的函数

如何使变量 mq 可访问?

2 个答案:

答案 0 :(得分:0)

您必须在包含类的构造函数中创建message queue对象mq

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

并且您必须使用initalizer list来执行此操作,因为消息队列对象(例如mq)有无法访问default constructor 。这是构造函数的右大括号后编译器消息C2248的含义。

BTW:在常规方法中永远不能初始化成员,这是编译器在Open方法中发现的错误(因此是C2064)。其中还有一些其他错误(或误解或开放式结果),而对mq.send的调用将按预期工作(至少一次)。


[更新]

或者,您可以使用堆栈上的变量访问boost的消息队列:

/// 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);
}

......我没试过这个,但如果不可能的话,the static remove method没有多大意义。

答案 1 :(得分:0)

此处的错误意味着构造函数是私有的,因此无法调用。此外,由于您没有显式调用它,因此默认构造函数将作为CMsqQueueMngr的一部分进行调用。由于它(可能)是故意私有的,你需要调用适当的构造函数或类似的东西,但是你试图以不打算使用的方式使用该类。解决方案是研究说明。从那些,应该清楚该做什么,即通过初始化列表传递正确的参数:

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

注意:

  • 由于代码现在只接收到ctor的一个参数,因此您应该将其设为explicit
  • 您不应使用指向(非constchar的指针作为名称,请使用std::string
  • 一般来说,研究“const correctness”的想法。
  • 您必须向构造函数提供queueName。如果您不想这样,则必须动态分配队列。
  • 作为替代方案,您可以在需要时创建消息队列的本地实例,但这听起来不是一个好主意。您需要根据您的设计决定。
  • 您的邮件似乎是uint8_t,所以为什么您的邮件大小配置为int的大小?
  • 避免使用像100这样的“魔法数字”。
  • int mode也是一个糟糕的选择,如果这就是它的意思,请使用枚举。
  • 不要仅捕获异常以返回-1(另一个幻数)。错误代码很糟糕,除非你真的处理它,否则让异常飞起来。
  • 下次,缩进您的代码。毕竟,你希望人们阅读它,对吗?
相关问题