以下是一个给出编译时错误的程序。这主要是在D类中使用函数Boo。我最终尝试使用多个线程来调用solve方法,但这对我来说似乎并没有太好用到达到目前为止。
错误是:
1>d:\dummy\project1\trash.cpp(37): warning C4101: 'd': unreferenced local variable
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240): error C2672: 'std::invoke': no matching overloaded function found
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(248): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1>(std::tuple<void (__thiscall A::* )(C),C> &,std::integer_sequence<_Ty,0,1>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>,
1> _Ty=::size_t
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0,1>(std::tuple<void (__thiscall A::* )(C),C> &,std::integer_sequence<_Ty,0,1>)' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>,
1> _Ty=::size_t
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(244): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(232): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(259): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1> with
1> [
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thread(48): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<_Ty>>>(_Thrd_t *,_Target &&)' being compiled
1> with
1> [
1> _Ty=std::tuple<void (__thiscall A::* )(C),C>,
1> _Target=std::unique_ptr<std::tuple<void (__thiscall A::* )(C),C>,std::default_delete<std::tuple<void (__thiscall A::* )(C),C>>>
1> ]
1>d:\trash\project1\trash.cpp(26): note: see reference to function template instantiation 'std::thread::thread<void(__thiscall A::* )(C),C&,void>(_Fn &&,C &)' being compiled
1> with
1> [
1> _Fn=void (__thiscall A::* )(C)
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240): note: With the following template arguments:
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240): note: '_Callable=void (__thiscall A::* )(C)'
1>c:\program files (x86)\microsoft visual studio\2017\community1\vc\tools\msvc\14.11.25503\include\thr\xthread(240): note: '_Types={C}'
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
代码是:
class C {
};
class A {
public:
virtual void solve(C c) = 0;
};
class B:A {
public:
void solve(C c) {};
};
class D {
public:
void Boo(B* b, C &c)
{
auto thread1 = std::thread(&A::solve,c);
thread1.join();
}
};
int main()
{
B b;
C c;
D d;
}
感谢您的时间! :)
答案 0 :(得分:0)
问题是你试图在你的上下文中使用错误的参数调用void Container::DrawContainer()
*这是类型Screen
而是void Container::DrawContainer()
,除了接收{{1}类型的参数1}}。
你可能想做的是:
Container
答案 1 :(得分:0)
您需要提供一个对象来调用非静态方法,例如:
auto thread1 = std::thread(&A::solve, b, c);
但由于该方法不匹配,因此无法编译。但您可以将b
投射到(A*)
或将Boo
的参数更改为A* a
。
void Boo(A* a, C &c)
{
auto thread1 = std::thread(&A::solve, a, c);
thread1.join();
}
您可以查看http://en.cppreference.com/w/cpp/utility/functional/invoke处的示例。