开发者

Implementing and calling a static method in a template class

开发者 https://www.devze.com 2023-03-11 11:21 出处:网络
My code is as below: Interpolation.h #ifndef INTERPOLATOR #define INTERPOLATOR #include <vector> #include <utility>

My code is as below:

Interpolation.h

#ifndef INTERPOLATOR
#define INTERPOLATOR

#include <vector>
#include <utility>

template <class T>
class Interpolator
{
    public:
        static T InterpolateVector(const std::vector<std::pair<T, T>> & Vector, T At);

    private:
        static T GetBasisValue(T x);
};

template <class T>
T Interpolator::InterpolateVector(const std::vector<std::pair<T, T>> & Vector, T At) // Line #22
{
    // ...
}   // Line #25

// ...

#endif  // INTERPOLATOR

main.cpp

#include <vector>
#include <utility>
#include "Interpolator.h"

int wmain(int argc, wchar_t *argv[])
{
    std::vector<std::pair<float, float>> Measurements;
    Measurements.push_back(std::make_pair(0, 80.8));
    Measurements.push_back(std::make_pair(1, 80开发者_如何学编程.4));
    Measurements.push_back(std::make_pair(3, 80.1));
    Measurements.push_back(std::make_pair(4, 79.6));

    float y2 = Interpolator<float>::InterpolateVector(Measurements, 2.0f);

    return 0;
}

When I build this code, I get the following error messages:

C:...\Interpolator.h; Line #22

error C2955: 'Interpolator' : use of class template requires template argument list

C:...\Interpolator.h; Line #25

error C2244: 'Interpolator::InterpolateVector' : unable to match function definition to an existing declaration

Can anyone please tell me what I am doing wrong here?

(IDE: Visual Studio 2010 Ultimate)


As written in the error message: 'Interpolator' : use of class template requires template argument list

You should write:

template <class T>
T Interpolator<T>::InterpolateVector(const std::vector<std::pair<T, T>> & Vector, T At) // Line #22
{
    // ...
}   // Line #25


#ifndef INTERPOLATOR
#define INTERPOLATOR

#include <vector>
#include <utility>

template <class T>
class Interpolator
{
    public:
        static T InterpolateVector(const std::vector<std::pair<T, T> > & Vector, T At);

    private:
        static T GetBasisValue(T x);
};

template <class T>
T Interpolator <T> ::InterpolateVector(const std::vector<std::pair<T, T> > & Vector, T At) // Line #22
{
    // ...
}   // Line #25
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号