c++ - 使用 catch2 进行结构和测试的问题
问题描述
所以我在 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));
}
}
}
}
但是当我编译这个时,我得到以下错误:
错误:“struct ASSplinePath::VectorXY”没有名为“create_x_y_vectors”的成员 xy_vec.create_x_y_vectors(points);
错误:没有匹配函数调用'Catch::Detail::Approx::Approx(double, double, double)' REQUIRE(xy_vec.x == Approx(1.0, -3.0, 5.0));
我应该补充一点,当我注释掉这个测试时,一切都编译得很好。因此,我认为这里有问题。因此,我不太确定这个错误意味着什么。我将衷心感谢您的帮助。谢谢你。
解决方案
所以我终于弄清楚了问题所在。如果有人遇到类似类型的问题,请确保该函数是在结构中声明的,还是在其他类中声明的。在我的情况下,以下工作:
xy_vec = Point2D<double>().create_x_y_vectors(points);
create_x_y_vectors() 是在 Point2D 类中定义的,因此有必要制作一个 Point2D 对象。
推荐阅读
- java - 在运行时在 JPA Spring JPARepository 中实现 customDao
- ruby-on-rails - 无法自动加载常量 A,预期
定义它 - javascript - 如何将最后 3 个随机数存储在 3 个不同的变量 javascript 中?
- python - 使用随机值复制字典
- jquery - 循环遍历数据行/页面并进行 ajax 调用以在循环结束时获取新数据,然后再次开始循环
- python - 如何聚合多个列并输出为行?
- django - 如果它们已经存在于数据库中,则选择过滤和删除
- javascript - 我应该使用状态还是调用函数以获得更好的性能?
- microsoft-graph-api - 像 Google 这样的更简单的 MSAL.js 身份验证?(不使用缓存系统)
- javascript - 如何