静态模板函数在类中 [英] static template functions in a class

查看:185
本文介绍了静态模板函数在类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 模板< typename T> double foo(vector& arr); 


解决方案

在.h文件中定义函数。 p>

适合我使用



ah

  #include< vector> 
#include< iostream>

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

};

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

main.cpp

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


How do I make the following function inside a class and then access this function from main? My class is just a collection of a bunch of static functions.

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

解决方案

Define the function in the .h file.

Works fine for me

a.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 );
    }

这篇关于静态模板函数在类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆