因此,我在point2D.h头文件中有以下函数:
VectorXY<T> ASSplinePath::Point2D<T>::create_x_y_vectors(const std::vector<Point2D<T>>& point_vector)然后,在point2D.cpp文件中,我按照以下方式使用该函数:
template <typename T>
VectorXY<T> ASSplinePath::Point2D<T>::create_x_y_vectors(const std::vector<Point2D<T>>& point_vector)
{
VectorXY<T> xy_vec;
size_t vec_length = point_vector.size();
// Preallocate the vector size
xy_vec.x.resize(vec_length);
xy_vec.y.resize(vec_length);
for(size_t i = 0; i < vec_length; ++i){
xy_vec.x[i] = point_vector[i].x();
xy_vec.y[i] = point_vector[i].y();
}
return xy_vec;
}在cpp文件末尾还包括以下内容:
template class ASSplinePath::Point2D<float>;
template class ASSplinePath::Point2D<double>;这里,VectorXY是一个在另一个头文件中定义的结构。因此,我在point2D.h和point2D.cpp文件中都包含了这个头文件。
template <typename T> struct VectorXY {
std::vector<T> x;
std::vector<T> y;
};这里,point_vector来自一个不同的点类。
为了测试这个函数,我用catch2和BDD风格编写了下面的测试。
SCENARIO("Creating x and y vectors from a vector of Point2D")
{
GIVEN("A Vector of Point2D<double> object")
{
std::vector<Point2D<double>> points;
Point2D<double> point_1(1.0, 2.0);
Point2D<double> point_2(-3.0, 4.0);
Point2D<double> point_3(5.0, -6.0);
points.push_back(point_1);
points.push_back(point_2);
points.push_back(point_3);
VectorXY<double> xy_vec;
WHEN("Creating x and y vectors")
{
xy_vec.create_x_y_vectors(points);
THEN("x and y vector should be returned")
{
REQUIRE(xy_vec.x == Approx(1.0, -3.0, 5.0));
REQUIRE(xy_vec.y == Approx(2.0, 4.0, -6.0));
}
}
}
}但是,当我编译它时,我会得到以下错误:
error:‘struct::VectorXY’没有名为‘create_x_y_vectors’xy_vec.create_x_y_vectors(点数)的成员;
error:对“xy_vec.x == Approx(1.0,-3.0,5.0)”的调用没有匹配的函数(xy_vec.x==Approx(1.0,-3.0,5.0);
我应该补充一下,当我注释掉这个测试时,所有的东西都编译得很好。因此,我想这里一定有什么不对劲。因此,我不太清楚这个错误意味着什么。我非常感谢你的帮助。谢谢。
发布于 2020-05-01 04:31:56
所以我终于弄明白问题出在哪里了。如果遇到类似类型的问题,请确保函数是在结构中声明的,还是在其他类中声明的。在我的情况下,以下工作:
xy_vec = Point2D<double>().create_x_y_vectors(points);create_x_y_vectors()是在Point2D类中定义的,因此有必要创建一个Point2D对象。
https://stackoverflow.com/questions/61458113
复制相似问题