加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 模板问题

发布时间:2020-12-16 10:42:44 所属栏目:百科 来源:网络整理
导读:为什么我能做到这一点: #include iostream#include algorithm#include vectorusing namespace std;void myfunction (int i) { cout " " i;}int main () { vectorint myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); co
为什么我能做到这一点:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void myfunction (int i) {
  cout << " " << i;
}

int main () {
  vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  cout << "myvector contains:";
  for_each (myvector.begin(),myvector.end(),myfunction);//<-------See below
return 0;
}

但不能这样做:

template<class T>
    void myfunction (T i) {
          cout << " " << i;
        }

我怀疑它与args演绎有关,但令人气愤的是“常规”fnc被接受而模板不被接受.

解决方法

问题是您无法创建指向模板函数的指针.您应该能够创建指向实例化模板函数的指针.我没试过这个,但以下应该有效:

for_each (myvector.begin(),myfunction<int>)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读