私は以下の報告のものと同様のエラーメッセージでC++ thread error: no type named typeというページを見つけました。そのページの答えは、私が知る限り、このケースをカバーしていません。確かに、ここで私は単純に無関心な何かがなければならない。C++スレッドエラー - クラス引数付きコンパイル時エラー
私は、私が取り組んでいるC++プログラムでスレッドを使用しようとしています。私はboost::thread
と一緒に作業していた初期バージョンを持っていましたが、何の問題もありませんでした。今朝、boost::thread
の代わりにstd::thread
を使用するようにコードを書き直そうとしました。それは私が突然私が理解していないコンパイル時のエラーに遭遇したときです。私はこの問題を次のコードに還元しました。
アップショット?関数引数として自分自身のユーザー定義クラスのいずれかへの参照を渡そうとすると、プログラムはコンパイルに失敗します。
#include <iostream>
#include <thread>
class TestClass { } ;
void testfunc1 (void) { std::cout << "Hello World TF1" << std::endl ; }
void testfunc2 (double val) { std::cout << "Hello World TF2" << std::endl ; }
void testfunc3 (TestClass & tc) { std::cout << "Hello World TF3" << std::endl ; }
int main (int argc, char *argv[])
{
std::thread t1 (testfunc1) ;
double tv ;
std::thread t2 (testfunc2, tv) ;
TestClass tc ;
std::thread t3 (testfunc3, tc) ; // compiler generates error here
return 0 ;
}
最後のコード行をコメントアウトしている限り、コードはコンパイルされます。しかし、それが存在すると、次のコンパイル時エラーが発生します。
$ g++ -std=c++11 test.cpp
In file included from /usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/thread:39:0,
from test.cpp:3:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/functional: In instantiation of ‘struct std::_Bind_simple<void (*(TestClass))(TestClass&)>’:
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/thread:142:59: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(TestClass&); _Args = {TestClass&}]’
test.cpp:19:33: required from here
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/functional:1505:61: error: no type named ‘type’ in ‘class std::result_of<void (*(TestClass))(TestClass&)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/include/c++/functional:1526:9: error: no type named ‘type’ in ‘class std::result_of<void (*(TestClass))(TestClass&)>’
_M_invoke(_Index_tuple<_Indices...>)
明らかに何らかの種類の問題がありますが、このエラーメッセージを解読することはできません。問題は何ですか? (私は、Windows 10マシン上でCygwinを使用することが起こる、私はそれを説明した問題に関連はないと思う。)
ありがとう!それは自分では決して考えなかったでしょう。とても有難い! –