c++ - 类中的静态模板函数

标签 c++ class templates static typename

如何在类中创建以下函数,然后从 main 访问该函数? 我的类只是一堆静态函数的集合。

template<typename T> double foo(vector<T> arr);

最佳答案

在 .h 文件中定义函数。

适合我

啊。

#include <vector>
#include <iostream>

using namespace std;
class A {
public:
template< typename T>
    static double foo( vector<T> arr );

};

template< typename T>
double A::foo( vector<T> arr ){ cout << arr[0]; }

main.cpp

#include "a.h"
int main(int argc, char *argv[])
{
    A a;
    vector<int> arr;
    arr.push_back(1);
    A::foo<int> ( arr );
}

 

关于c++ - 类中的静态模板函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9346076/

相关文章:

c++ - 如何在 C++ 中获取子字符串并在字符串之间添加字符

Python Airflow 自定义传感器 - 实现哪些方法

python - Flask 不渲染模板,可能是文件结构的问题

c++ - 如何重构具有相同行的函数,唯一不同的是一个函数调用?

c++ - 模板比较运算符

c++ - 无法将 “member pointer to derived class”转换为 “member pointer to base class”

c++ - 在 Visual Studio 2010 中包含 fwpmu.h 时出错

c++ - float 和 double 的大小

Java 无法使 TempListener 工作

java - 抽象类构造函数初始化