首页 > 解决方案 > 模板函数未在此范围内声明

问题描述

我有一个 SplineCreator 类的头文件和一个 cpp 文件。这里面有一个函数叫做 CreateLinearSpline 和 CreateCubicSpline。这里:-(注意:这不是一个完整的头文件,但这些是我感兴趣的函数。我也在 cpp 文件中定义了这些函数。所有的头文件都包含在顶部)

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

}

我在另一个 cpp 文件中使用了这些函数,如下所示:

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

}

另外,我已经在这个文件的顶部包含了所有的头文件。但是当我使用 catkin_make 编译它时,出现以下错误:

错误:未在此范围内声明“CreateLinearSpline” x_spline = CreateLinearSpline(xy_vec.x, x_diff);

错误:未在此范围内声明“CreateLinearSpline”,并且在实例化点通过依赖于参数的查找未找到任何声明 [-fpermissive] y_spline = CreateLinearSpline(xy_vec.y, x_diff);

我检查了类似的问题,但找不到解决方案。我确信我在某个地方犯了一个愚蠢的错误,但我似乎无法弄清楚这一点。

我希望有一个人可以帮助我。

标签: c++templates

解决方案


推荐阅读