2015-12-15 21 views
0

我目前正在C++應用程序,我需要創建一個模塊,發送提升信號到另一個類。我使用的文檔,查看示例作爲我的應用程序(http://www.boost.org/doc/libs/1_55_0/doc/html/signals2/tutorial.html#signals2.tutorial.document-view)的基礎,但我不斷收到一個錯誤:提升信號2給不可複製的錯誤

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(TrialModel),一個簡單的邏輯計數+1每500毫秒和交流的典範一個出站邏輯適配器,可通過邏輯的boost信號庫訪問。

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函數,並且在我試圖將系統綁定在一起之前工作正常。

有什麼建議嗎?

回答

0

這裏是失敗的原因:

你​​類中有boost.signal類型的成員,並且該類型不是可複製的(它是從signal_base,其從nocopyable繼承繼承)。這使得類​​本身不可複製。然而,你正試圖在這裏將它複製:

TrialLogic logic; 
boost::thread t(logic); 

Boost.thread接受由值參數,所以你要複製的東西是不可拷貝和編譯器是無奈之舉。

至於解決方案,最簡單的似乎是:不是在​​上定義operator(),而是定義一個函數,而不是傳遞該函數以及指向邏輯類的指針或引用。類似的東西:

class TrialLogic 
{ 
... 
void launch() { 
    boost::this_thread::sleep(boost::posix_time::milliseconds(500)); 
    testNum++; 
    emitSignal(); 
} 
}; 
... 
// in main 
boost::thread(&TrialLogic::launch, &logic); 

最後,但並非最不重要,我不建議使用boost :: thread。 C++ 11線程支持完全可操作,而boost :: thread並沒有真正增加任何好處。

+0

哇 - 這解釋了很多!但是,如果我不能複製它,有沒有更新它的方式來運行線程? –

+0

@RainerKeerdo,我提供了一個建議。 – SergeyA

+0

抱歉,我在寫評論時一定會發布它!但是,我會嘗試使用std ::線程 - 然後我在以前的項目中總是使用boost線程。 –