gpt4 book ai didi

c++ - 在模板类中实现和调用静态方法

转载 作者:行者123 更新时间:2023-11-30 00:40:22 25 4
gpt4 key购买 nike

我的代码如下:

插值.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;
}

当我构建这段代码时,我收到以下错误消息:

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

谁能告诉我我做错了什么?

(IDE:Visual Studio 2010 旗舰版)

最佳答案

如错误消息中所写:'Interpolator' : use of class template requires template argument list

你应该写:

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

关于c++ - 在模板类中实现和调用静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6266544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com