我目前正在开发一个C++应用程序,在该应用程序中,我需要创建一个模块,该模块将向另一个类发送boost信号。我正在使用Document示例作为我的应用程序(0/doc/html/signals2/tutorial.html#signals2.tutorial.document-view)的基础,但是我一直收到一个错误:
Error 1 error C2280: boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable &)' : attempting to reference a deleted function这让我完全不知所措--错误实际上发生在哪里?
构建日志如下:
1>------ Build started: Project: 32BitTrial, Configuration: Debug Win32 ------
1> InboundLogicAdaptor.cpp
1> main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xlocmon(232): error C2280: 'boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable &)' : attempting to reference a deleted function
1> C:\boost_1_58_0\boost/core/noncopyable.hpp(34) : see declaration of 'boost::noncopyable_::noncopyable::noncopyable'
1> This diagnostic occurred in the compiler generated function 'boost::signals2::signal_base::signal_base(const boost::signals2::signal_base &)'
1> OutboundLogicAdaptor.cpp
1> TrialLogic.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========我的主要功能相当简单--我构建了一个GUI,一个用于与GUI (TrialModel)通信的模型,一个每500毫秒计数一次的简单逻辑,以及一个从逻辑中通过boost signals2库访问的出站逻辑适配器。
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TrialModel m;
Trial w(0, &m);
w.show();
TrialLogic logic;
OutboundLogicAdaptor adaptor(&m, logic);
boost::thread t(logic);
a.exec();
t.join();
return 1;
}逻辑类定义了一个信号,该信号有一个参数(整数)和一个操作符()作为线程。
TrialLogic.h:
#pragma once
#include <boost\thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost\signals2.hpp>
class TrialLogic
{
public:
typedef boost::signals2::signal<void(int x)> signal_t;
void operator()();
TrialLogic();
~TrialLogic();
boost::signals2::connection connect(const signal_t::slot_type &subscriber);
void doubleIncrementSlot();
private:
void emitSignal();
signal_t signal;
int testNum;
};守则本身:
#include "TrialLogic.h"
TrialLogic::TrialLogic()
{
testNum = 0;
}
TrialLogic::~TrialLogic()
{
}
boost::signals2::connection TrialLogic::connect(const signal_t::slot_type &subscriber){
return signal.connect(subscriber);
}
void TrialLogic::operator()(){
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
testNum++;
emitSignal();
}
void TrialLogic::emitSignal(){
signal_t(testNum);
}最后,接收信号的适配器-
#include "OutboundLogicAdaptor.h"
OutboundLogicAdaptor::OutboundLogicAdaptor(TrialModel *modelHook, TrialLogic &logicHook) : logic(logicHook)
{
this->hook = modelHook;
signalConnection = logic.connect(boost::bind(&OutboundLogicAdaptor::transmitAngle, this, _1));
}
OutboundLogicAdaptor::~OutboundLogicAdaptor()
{
signalConnection.disconnect();
}
void OutboundLogicAdaptor::transmitAngle(int angle){
hook->postAngle(angle);
}从我的第一次检查开始,我就找不到我做错了什么,但是很明显,我的代码有一个关键的错误。我非常肯定,这个问题也不在GUI方面,因为我实际上没有使用任何boost函数,而且在我试图将系统绑定到一起之前,它运行得很好。
有什么建议吗?
发布于 2015-12-15 22:18:28
以下是失败的原因:
您的TrialLogic类中有一个类型为boost.signal的成员,此类型不可复制(它是从signal_base继承的,后者是从nocopyable继承的)。这使得类TrialLogic本身不可复制。但你却试图在这里复制它:
TrialLogic logic;
boost::thread t(logic);Boost.thread按值接受参数,因此您试图复制不可复制的内容,而编译器则会不高兴。
至于解决方案,最简单的方法似乎是:与其在TrialLogic上定义TrialLogic,不如定义一个函数,而不是传递该函数以及对逻辑类的指针或引用。就像这样:
class TrialLogic
{
...
void launch() {
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
testNum++;
emitSignal();
}
};
...
// in main
boost::thread(&TrialLogic::launch, &logic);最后,但并非最不重要的是,我不建议使用boost::线程。C++11线程支持是完全可操作的,boost::线程并没有真正增加任何好处。
https://stackoverflow.com/questions/34300118
复制相似问题