繁体   English   中英

再次出现错误C2248

[英]Again on error C2248

错误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> :: message_queue_t':无法访问在类'boost :: interprocess :: message_queue_t>中声明的私有成员

错误C2064:术语未评估为带有4个参数的函数

如何使变量mq可访问?

您必须在包含类的构造函数中创建消息队列对象mq

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

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

顺便说一句:成员永远不能在常规方法中初始化,这就是编译器在Open方法中发现错误的地方(因此C2064)。 其中还有一些其他错误(或误解或断头),而对mq.send的调用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);
}

...我没有尝试过,但是如果不可能的话, 静态remove方法就没有多大意义。

这里的错误意味着构造函数是私有的,因此无法调用。 此外,由于您没有显式调用它,因此默认构造函数被称为CMsqQueueMngr的一部分。 由于它(可能)是有意私有的,因此您需要调用适当的构造函数或类似的构造函数,但是您试图以不希望使用的方式使用该类。 解决方案是研究说明。 从中,应该清楚该怎么做,即通过初始化列表传递适当的参数:

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

笔记:

  • 由于代码现在仅接收ctor的单个参数,因此您应该使其explicit
  • 您不应使用指向(non- constchar的指针作为名称,而应使用std::string
  • 通常,研究“常量正确性”的概念。
  • 您必须将queueName提供给构造函数。 如果您不想这样做,则必须动态分配队列。
  • 或者,您可以在需要时创建消息队列的本地实例,但这听起来不是一个好主意。 您需要根据设计来决定。
  • 看来您的消息是uint8_t ,那么为什么将消息大小配置为int大小?
  • 避免使用100等“魔术数字”。
  • int mode也是一个不好的选择,如果这是应该的话,请使用枚举。
  • 不要仅捕获异常以返回-1(另一个幻数)。 错误代码很糟糕,除非真正处理异常,否则就让它运行。
  • 下次,请缩进正确的代码。 毕竟,您希望人们阅读它,对吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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