简体   繁体   中英

Problems with Structs and tests with catch2

So I have the following function in point2D.h header file:

VectorXY<T> ASSplinePath::Point2D<T>::create_x_y_vectors(const std::vector<Point2D<T>>& point_vector)

Then in the point2D.cpp file I use this function as follows:

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;

}

Also at the end of the cpp file following is included:

template class ASSplinePath::Point2D<float>;
template class ASSplinePath::Point2D<double>;

Here VectorXY is a struct which is defined in another header file. Therefore, I have included this header file in both the point2D.h and point2D.cpp files.

template <typename T> struct VectorXY {

    std::vector<T> x;
    std::vector<T> y;
};

Here point_vector comes from a different point class.

To test this function I have written the following test with catch2 and BDD style.

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));
            }
        }

    }
}

But when I compile this, I get following errors:

error : 'struct ASSplinePath::VectorXY' has no member named 'create_x_y_vectors' xy_vec.create_x_y_vectors(points);

error : no matching function for call to 'Catch::Detail::Approx::Approx(double, double, double)' REQUIRE(xy_vec.x == Approx(1.0, -3.0, 5.0));

I should add, When I comment out the this test then everything compiles well. Hence, I assume something is wrong here. Hence, I am not quite sure what this error means. I would really appreciate your help. Thank You.

So I finally figured out what the problem was. If anyone comes across a question of similar type then make sure that the function is declared in the struct or is it declared in some other class. In my case following worked:

xy_vec = Point2D<double>().create_x_y_vectors(points);

create_x_y_vectors() was defined in Point2D class and hence it was necessary to make a Point2D Object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM