我需要在線程啓動時啓動一個將複雜參數(std :: thread <>)作爲參數的線程。我正在使用`std :: ref。此代碼可以在更新的環境中正常工作(在Ubuntu上運行g ++ - 4.8.2)。C++將引用變量傳遞給g ++中的線程 - 4.7.4
現在我不得不在舊的編譯器(g ++ 4.7.4)中編譯相同的代碼,並且出現錯誤。
的代碼如下所示,以及該錯誤:
ReaderThread.hpp
class ReaderThread {
void start(Reader reader, SyncController &syncController);
}
ReaderThread.cpp
void ReaderThread::start(Reader reader, SyncController &syncController)
{
Do something...
}
的main.cpp
int main()
{
...do stuff...
/*
* Create and start the reader thread. The created object must live
* during the whole thread life.
* std::ref is used to pass as reference
*/
myReader = ReaderFactory(params);
std::shared_ptr<ReaderThread> ptr(new ReaderThread);
std::thread th(&ReaderThread::start, ptr, myReader, std::ref(syncController));
...do other stuff...
}
錯誤:
In file included from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/move.h:57:0,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_pair.h:61,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_algobase.h:65,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/char_traits.h:41,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ios:41,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ostream:40,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/iostream:40,
from ./main.cpp:11:
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits: In instantiation of 'struct std::_Result_of_impl<false, false, std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>, std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController> >':
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1857:12: required from 'class std::result_of<std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>(std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController>)>'
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/functional:1563:61: required from 'struct std::_Bind_simple<std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>(std::shared_ptr<aeirtuthread::ReaderThread>, Reader, std::reference_wrapper<SyncController>)>'
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/thread:133:9: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (ReaderThread::*)(Reader, SyncController&); _Args = {std::shared_ptr<ReaderThread>&, Reader&, std::reference_wrapper<SyncController>}]'
./aeirtu/aeirtu/main.cpp:155:96: required from here
/usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1834:9: error: no match for call to '(std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>) (std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController>)'
我不能看看這個錯誤正在對舊的編譯器或不同的東西因使用的std::ref
。
幫助我們找到一個將被4.7.4支持的編譯器並編譯我的代碼。
雖然C++ 11在gcc 4.8.x中幾乎完全支持,但gcc 4.7.x缺少一些功能。在這裏檢查:https://gcc.gnu.org/projects/cxx0x.html – SHR
確實,但我需要修復代碼編譯...幫助讚賞... – Mendes
使用指針,而不是參考。 –