Error : cannot convert ‘std::vector<float>’ to ‘float’ in initialization
Error : cannot convert ‘std::vector<float>’ to ‘float’ in initialization
我已将 boundary_info
结构的向量定义为 std::vector<boundary_info> nodes
以在我的代码中用于特定目的。当我尝试在特定函数中将 push_back
新元素添加到此向量中时:
void myFun()
{
std::vector<float_type> dists(9, -1.0);
std::array<float_type,9> f, g;
//do something - x and y are defined here
nodes.push_back(boundary_info{point<int>{x,y}, dists, f, g, {}});
}
我收到以下错误消息:
Error 1 : cannot convert ‘std::vector<float>’ to ‘float’ in initialization
Error 2 : cannot convert ‘std::array<float, 9ul>’ to ‘float’ in
initialization
Error 3 : cannot convert ‘std::array<float, 9ul>’ to ‘float’ in
initialization
错误 1 与 dists
相关联,它是一个向量。错误 2 和 3 分别与在 push_back
中作为参数传递的 f, g
相关联。
代码如下所示。
#include <iostream>
#include <vector>
template <typename T>
struct point //specify a point structure
{
T x,y;
};
struct boundary_info
{
point<int> xy_bdary; //coordinates of a bdary point
std::array<float_type,9> dist; //distance from boundary
std::array<float_type,9> f_prev, g_prev; //populations
std::vector<int> miss_dirns; //missing directions
};
如果能指出此错误的解决方案,我将很高兴。半天以来,我一直在为此苦苦挣扎。
注意 :我正在使用 c++11 进行编译。
编辑
您可以在以下位置找到重现相同问题的此问题的最少代码
https://repl.it/repls/GleefulTartMarkuplanguage
谢谢
在下一行中,您尝试从 std::vector
(dists
) 初始化 std::array
(boundary_info::dist
):
nodes.push_back(boundary_info{point<int>{x,y}, dists, f, g, {}});
std::array
没有接受 std::vector
的构造函数。您只能按元素初始化 std::array
(聚合初始化)或将 std::vector
显式复制到 std::array
。
聚合初始化
nodes.push_back(boundary_info{point<int>{x,y}, {dists[0], dists[1], dists[2], dists[3], dists[4], dists[5], dists[6], dists[7], dists[8]}, f, g, {}});
当然,这不是很优雅。
复制std::vector
到std::array
借助一点模板功能,我们可以做得更好。
template<typename T, std::size_t N, typename Range>
std::array<T,N> to_array( Range const& in )
{
std::array<T,N> result;
// To make the standard begin() and end() in addition to any user-defined
// overloads available for ADL.
using std::begin; using std::end;
std::copy( begin( in ), end( in ), result.begin() );
return result;
}
to_array
接受具有 begin()
和 end()
成员函数或自由函数重载的任何输入类型 begin()
和 end()
.
现在您可以像这样从向量初始化数组:
nodes.push_back(boundary_info{point<int>{x,y}, to_array<float_type,9>(dists), f, g, {}});
请注意,如果 dists
的元素多于数组,你很容易搬起石头砸自己的脚,因为 to_array
不做任何范围检查(std::copy
不做做任何一个)。如果需要,我会把它留作 reader 的练习,以使函数更安全。
我已将 boundary_info
结构的向量定义为 std::vector<boundary_info> nodes
以在我的代码中用于特定目的。当我尝试在特定函数中将 push_back
新元素添加到此向量中时:
void myFun()
{
std::vector<float_type> dists(9, -1.0);
std::array<float_type,9> f, g;
//do something - x and y are defined here
nodes.push_back(boundary_info{point<int>{x,y}, dists, f, g, {}});
}
我收到以下错误消息:
Error 1 : cannot convert ‘std::vector<float>’ to ‘float’ in initialization
Error 2 : cannot convert ‘std::array<float, 9ul>’ to ‘float’ in
initialization
Error 3 : cannot convert ‘std::array<float, 9ul>’ to ‘float’ in
initialization
错误 1 与 dists
相关联,它是一个向量。错误 2 和 3 分别与在 push_back
中作为参数传递的 f, g
相关联。
代码如下所示。
#include <iostream>
#include <vector>
template <typename T>
struct point //specify a point structure
{
T x,y;
};
struct boundary_info
{
point<int> xy_bdary; //coordinates of a bdary point
std::array<float_type,9> dist; //distance from boundary
std::array<float_type,9> f_prev, g_prev; //populations
std::vector<int> miss_dirns; //missing directions
};
如果能指出此错误的解决方案,我将很高兴。半天以来,我一直在为此苦苦挣扎。
注意 :我正在使用 c++11 进行编译。
编辑 您可以在以下位置找到重现相同问题的此问题的最少代码 https://repl.it/repls/GleefulTartMarkuplanguage
谢谢
在下一行中,您尝试从 std::vector
(dists
) 初始化 std::array
(boundary_info::dist
):
nodes.push_back(boundary_info{point<int>{x,y}, dists, f, g, {}});
std::array
没有接受 std::vector
的构造函数。您只能按元素初始化 std::array
(聚合初始化)或将 std::vector
显式复制到 std::array
。
聚合初始化
nodes.push_back(boundary_info{point<int>{x,y}, {dists[0], dists[1], dists[2], dists[3], dists[4], dists[5], dists[6], dists[7], dists[8]}, f, g, {}});
当然,这不是很优雅。
复制std::vector
到std::array
借助一点模板功能,我们可以做得更好。
template<typename T, std::size_t N, typename Range>
std::array<T,N> to_array( Range const& in )
{
std::array<T,N> result;
// To make the standard begin() and end() in addition to any user-defined
// overloads available for ADL.
using std::begin; using std::end;
std::copy( begin( in ), end( in ), result.begin() );
return result;
}
to_array
接受具有 begin()
和 end()
成员函数或自由函数重载的任何输入类型 begin()
和 end()
.
现在您可以像这样从向量初始化数组:
nodes.push_back(boundary_info{point<int>{x,y}, to_array<float_type,9>(dists), f, g, {}});
请注意,如果 dists
的元素多于数组,你很容易搬起石头砸自己的脚,因为 to_array
不做任何范围检查(std::copy
不做做任何一个)。如果需要,我会把它留作 reader 的练习,以使函数更安全。