在模板类中实现和调用静态方法
Implementing and calling a static method in a template class
我的代码如下:
插值.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: 。。。\插值器.h;第22行
错误C2955:"插值器":使用类template需要template参数列出C: 。。。\插值器.h;第25行
错误C2244:'插值器::插值矢量':无法将函数定义与匹配现有的申报
有人能告诉我我在这里做错了什么吗?
(IDE:Visual Studio 2010 Ultimate)
如错误消息中所写:'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
#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
相关文章:
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 如何通过命名空间调用非静态方法
- 调用从模板派生的类的静态方法,而不指定模板
- 如何在 c++ 中异步调用静态方法?
- 从另一个标头中的标头调用静态方法
- LNK2019 在文件中调用静态成员的方法时出错.cpp
- 调用静态方法,收到错误:LNK2019
- 从线程内的对象调用静态方法
- C 调用静态方法
- 从指向实例的指针调用静态方法时出现链接错误
- JNI调用静态方法.类对象是必需的吗
- 正在构造函数中调用静态方法
- 无法在Qt中调用静态方法
- 通过重复对象名称来调用静态方法
- 在最终类中调用静态方法的静态基构造函数的设计模式
- 从C调用静态C++方法在编译时会给出未定义的引用
- 在全局范围内调用静态方法
- 是否可以获得调用静态方法的类
- 在模板类中实现和调用静态方法
- 确保在main()之前调用静态方法