0

I have a header and a cpp file of SplineCreator class. There is a function in this called CreateLinearSpline and CreateCubicSpline. Here:- (Note: This is not a complete header file but these are the functions I am interested about. I have defined these functions in the cpp file as well. Also all the header files are included on top)

template <typename T> class SplineCreator {
public:
    Spline<T> CreateLinearSpline(const std::vector<T>& y_values, const T x_diff = 1.0);

    Spline<T> CreateCubicSpline(const std::vector<T>& y_values,
        const std::array<T, 2> boundary_values = std::array<T, 2>{ { 0.0, 0.0 } },
        const std::array<BoundaryType, 2> boundary_type
        = std::array<BoundaryType, 2>{ { first_deriv, first_deriv } },
        const T x_diff = 1.0);

}

I have used these functions in another cpp file as follows:

template <typename T> Path<T>::Path(const std::vector<Point2D<T>>& point_vector, 
    const T x_diff, const T gradient_start, const T gradient_end, const size_t degree)

{

    VectorXY<T> xy_vec;

    Point2D<T>::create_x_y_vectors(point_vector);

    Expects(degree > 0);

    std::array<T, 2> boundary_values{ { gradient_start, gradient_end } };

    Spline<T> x_spline;
    Spline<T> y_spline;

    switch (degree) {

        case 1:

            x_spline = CreateLinearSpline(xy_vec.x, x_diff);
            y_spline = CreateLinearSpline(xy_vec.y, x_diff);

        case 3:

            x_spline = CreateCubicSpline(xy_vec.x, boundary_values,
                std::array<BoundaryType, 2>{ { first_deriv, first_deriv } }, x_diff);

            y_spline = CreateCubicSpline(xy_vec.y, boundary_values,
                std::array<BoundaryType, 2>{ { first_deriv, first_deriv } }, x_diff);

        default:

            throw std::invalid_argument("The Degree should be either 1 or 3");
    }


    Path(x_spline, y_spline);

}

Also, I have included all the header files on top of this file. But when I compile it using catkin_make I am getting the following error:

error: ‘CreateLinearSpline’ was not declared in this scope x_spline = CreateLinearSpline(xy_vec.x, x_diff);

error: ‘CreateLinearSpline’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] y_spline = CreateLinearSpline(xy_vec.y, x_diff);

I have checked similar questions but could not find the solution. I am sure I am making a silly error somewhere but I can't seem to figure this out.

I hope someone can help me.

y_1234
  • 61
  • 1
  • 7
  • 4
    `CreateLinearSpline()` is a non-static member function of the templated `SplineCreator`. That means it needs to be called using the syntax `some_object.CreateLinearSpline()` where `some_object` is an instance of (an instantiation of) the class. Your calling code is not providing an object. – Peter Apr 17 '20 at 13:42
  • @Scheff I checked my code and it does include a semicolon. I just forgot a semicolon here. I will edit the question. Thanks for pointing it out. – y_1234 Apr 17 '20 at 13:45
  • Btw. this might be of interest as well: [SO: Why can templates only be implemented in the header file?](https://stackoverflow.com/a/495056/7478597) – Scheff's Cat Apr 17 '20 at 13:47
  • @Peter SplineCreator object; and then object.CreateLinearSpline() ? And also, do instantiate this object above switch ? – y_1234 Apr 17 '20 at 13:48
  • @Scheff Even if write SplineCreator::CreateLinearSpline(), I still get the error – y_1234 Apr 17 '20 at 13:51
  • 1
    @yashrunwal Yes, they are non-static, namespace-access won't work here, you'll need an actual `SplineCreator` object. At least `SplineCreator().CreateLInearSpline(args)`. – bipll Apr 17 '20 at 13:53
  • 1
    `SplineCreator::CreateLinearSpline()` would be valid if `CreateLinearSpline()` would be a `static` member function but it isn't. Non-static functions may not be called without an object (on its left side). Concerning this, template classes are not distinct from "ordinary" classes. – Scheff's Cat Apr 17 '20 at 13:53
  • 1
    On a somewhat related note, why is `SplineCreator` a class at all? Does it have some internal state that you're not showing? (Don't fall into the "everything must be a member of a class" trap.) – molbdnilo Apr 17 '20 at 14:05
  • @Peter You were right. Wait now how do I accept the answer? Or close the question directly? – y_1234 Apr 17 '20 at 14:43
  • @molbdnilo I am sorry I cannot post the entire code here. I am not permitted to do that. – y_1234 Apr 17 '20 at 14:43

0 Answers0