没有匹配的函数调用错误C ++
我试图将一个元组的向量变成一个元组向量(反之亦然)。 无法调用tuple_transpose
函数。 当我用一个参数调用它时,我得到一个没有匹配的函数调用错误:
prog.cpp:在函数'int main()'中:
prog.cpp:44:24:错误:没有匹配函数调用'tuple_transpose(std :: tuple>,std :: vector>>&)'
prog.cpp:44:24:注意:候选人是:
prog.cpp:30:6:note:template typename transpose :: type tuple_transpose(std :: tuple> ...>&,seq)
prog.cpp:30:6:注意:模板参数推演/替换失败:
prog.cpp:44:24:注:候选人需要2个参数,1个提供
prog.cpp:36:6:note:template typename transpose :: type tuple_transpose(std :: tuple> ...>&)
prog.cpp:36:6:注意:模板参数推导/替换失败:
prog.cpp:代替'template typename transpose :: type tuple_transpose(std :: tuple> ...>&)[with T = {int,bool}]':
prog.cpp:44:24:从这里需要
prog.cpp:36:6:错误:在'struct transpose>,std :: vector>>&>'中没有类型命名'type'
#include <vector>
#include <tuple>
#include <type_traits>
template <typename... T>
struct transpose {};
template <typename... T>
struct transpose<std::tuple<std::vector<T>...>>
{
using type = std::vector<std::tuple<T...>>;
};
template <typename... T>
struct transpose<std::vector<std::tuple<T...>>>
{
using type = std::tuple<std::vector<T>...>;
};
// Indicies from Andy Prowl's answer
template <int... Is>
struct seq {};
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <int... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
template <typename... T, int... Is>
auto tuple_transpose(std::tuple<std::vector<T>...>& var, seq<Is...>) -> typename transpose<decltype(var)>::type
{
return { std::make_tuple(std::get<Is>(var)...) };
}
template <typename... T>
auto tuple_transpose(std::tuple<std::vector<T>...>& var) -> typename transpose<decltype(var)>::type
{
return tuple_transpose(var, gen_seq<sizeof...(T)>{});
}
int main()
{
std::tuple<std::vector<int>, std::vector<bool>> var;
tuple_transpose(var); // error
...
}
这是一个演示,其中有错误 - http://ideone.com/7AWiQQ#view_edit_box
我做错了什么,我该如何解决它? 谢谢。
如果您假定矢量大小相同,则应该完成这项工作:
template <int... Is>
struct seq {};
template <int N, int... Is>
struct gen_seq : gen_seq<N - 1, N - 1, Is...> {};
template <int... Is>
struct gen_seq<0, Is...> : seq<Is...> {};
template <typename... T, int... Is>
auto transpose(std::tuple<std::vector<T>...>& var, seq<Is...>)
-> std::vector<std::tuple<T...>>
{
std::vector<std::tuple<T...>> result;
for (std::size_t i = 0; i < std::get<0>(var).size(); i++)
{
std::tuple<T...> t = std::make_tuple(std::get<Is>(var)[i]...);
result.push_back(t);
}
return result;
}
template <typename... T, int... Is>
auto transpose(std::tuple<std::vector<T>...>& var)
-> std::vector<std::tuple<T...>>
{
return transpose(var, gen_seq<sizeof...(T)>());
}
以下是你如何测试它:
#include <iostream>
#include <iomanip>
int main()
{
std::vector<int> vi = {42, 1729, 6};
std::vector<bool> vb = {true, false, false};
std::vector<std::string> vs = {"Hi", "Hey", "Ho"};
auto t = make_tuple(vi, vb, vs);
auto v = transpose(t);
std::cout << std::boolalpha;
for (auto const& t : v)
{
std::cout << "(";
std::cout << std::get<0>(t);
std::cout << ", " << std::get<1>(t);
std::cout << ", " << std::get<2>(t);
std::cout << ")" << std::endl;
}
}
最后,一个生动的例子。
链接地址: http://www.djcxy.com/p/90137.html