0

尝试编写简单的代码来模拟工厂中的解析包。我正在发送一个保存在缓冲区中的包,例如:

发送类:

class PackageSender {
public:   
    void send_package();
    std::optional<Package> get_sending_buffer();
    void push_package(Package&&);
    std::optional<Package> sending_buffer;
public:
    ReceiverPreferences receiver_preferences;

};

    void PackageSender::push_package(Package &&p) {
    if(!sending_buffer){

        sending_buffer.emplace(std::move(p));
    }

}

使用功能:

void PackageSender::send_package() {

IPackageReceiver* receiver=receiver_preferences.choose_receiver(); //choose receiver from a list

   if(sending_buffer.has_value())
{
    receiver->receive_package(std::move(sending_buffer.value());
    sending_buffer.reset();
}
}

在将参数传递给对象的函数时:

`void Worker::receive_package(Package &&p) {


    queue_pointer->push(std::move(p));

    push_package(queue_pointer->pop());


  }`

包已将移动运算符定义为默认值:

Package& operator=(Package&&)= default;

下面的函数在传递对象时工作正常,例如:

Package p;    
receive_package(std::move(p));

似乎传递给函数的参数指向一个空的地方,如下所示:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

先感谢您。

4

0 回答 0