【问题标题】:Passing vectors of different types to a template function将不同类型的向量传递给模板函数
【发布时间】:2013-02-10 01:40:43
【问题描述】:

我收到一个编译器错误提示

could not deduce template argument for 'std::vector<T*>&' from 'std::vector<_Ty>'

template <typename T> void foo(vector<T*>& a, int left, int right)
{
     ...
}

main()
{
     ...
     //declare and instantiate 3 vectors
     vector<int> intVector;

     foo(intVector, 0, 100);
     foo(doubleVector, 0, 100);
     foo(charVector, 0, 100);
     ...
}

【问题讨论】:

  • vector&lt;T*&gt;&amp; 表示你想要一个指针向量,而你给它一个非指针向量。
  • 你为什么要做vector&lt;T*&gt; 而不仅仅是vector&lt;T&gt;

标签: c++ templates vector


【解决方案1】:

int 无法匹配 T*

要么将vector 设为vector&lt;int*&gt;,要么将模板设为vector&lt;T&gt;

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2017-12-25
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多