0
有沒有辦法讓這段代碼編譯?std :: bind不能用於字符串向量
1 #include <functional>
2 #include <vector>
3 #include <iostream>
4 using namespace std;
5 void f1(int x, int y){}
6 void f2(int x, vector<string> v) {}
7 int main()
8 {
9 f2(2, {{"hello", "it's", "me"}});
10 auto g1 = bind(f1, placeholders::_1, 3);
11 auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
12 return 0;
13 }
G1工作正常,G2給出了一些複雜的編譯錯誤
編輯: 徇衆要求我給你的錯誤:
In file included from test.cpp:1:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (*)(int, std::vector<std::__cxx11::basic_string<char> >)>’:
/usr/include/c++/5/functional:1439:12: required from ‘struct std::_Bind_helper<false, void (&)(int, std::vector<std::__cxx11::basic_string<char> >)>’
/usr/include/c++/5/functional:1462:5: required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}]’
test.cpp:11:67: required from here
/usr/include/c++/5/functional:1410:7: error: static assertion failed: Wrong number of arguments for function
static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
^
test.cpp: In function ‘int main()’:
test.cpp:11:67: error: no matching function for call to ‘bind(void (&)(int, std::vector<std::__cxx11::basic_string<char> >), const std::_Placeholder<1>&, <brace-enclosed initializer list>)’
auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
^
In file included from test.cpp:1:0:
/usr/include/c++/5/functional:1462:5: note: candidate: typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<void (*())(int, std::vector<std::__cxx11::basic_string<char> >)>]
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1462:5: note: candidate expects 1 argument, 3 provided
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
bind(_Func&& __f, _BoundArgs&&... __args)
^
/usr/include/c++/5/functional:1490:5: note: template argument deduction/substitution failed:
test.cpp:11:67: note: couldn't deduce template parameter ‘_Result’
auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
^
解決了這個問題,它需要'矢量 {{「你好」,「它是」,「我」}}'。只需要一些stackoverflow橡皮戳 –
titus
當提出有關編譯錯誤的問題時,請提供錯誤和行號。它會增加某人回答的機會。一般而言,您希望儘可能方便某人幫助您。如果您解決了問題,請將解決方案作爲答案發布,並接受以供將來的觀衆使用。 –
它不是一個字符串數組,而是一個字符串矢量,它有很大的不同,請編輯標題。 –