【问题标题】:Invalid, Wrong number template arguments error while using pair vectors使用对向量时出现无效、错误的数字模板参数错误
【发布时间】:2014-06-11 06:38:05
【问题描述】:

代码中有两种类型的错误,基于 向量 > vg(n) 我无法纠正

  1. 在最后一行,即; return 0 语句有一个错误,说“模板参数的数量错误(1,应该是 2)|和一个 Line:87 链接到 STL 库,上面写着“提供给‘template struct std::pair”
  2. 函数的第一行(第 7 行)表示模板参数无效

 #include <utility>
 #include <cmath>
 #include<cstdio>
 #include <vector>

 using namespace std

 #define COMP(a,b,xx,yy)(sqrt(((a-xx)*(a- xx)) + ((b-yy)*(b-yy))))

 double radius ( vector<pair<(int, int)> > vk, int ii, int n)
 {             //error:template argument 1,2 is invalid
 int d=n;
  int xx=vk[ii].first;
  int yy=vk[ii].second;
  int k = ii==0? 1:0;

  double small=COMP(vk[k].first,vk[k].second,xx,yy);
   double dd;
   for (int i=0;i<d; i++)
  {
   if (i!=ii)
   dd=COMP(vk[i].first,vk[i].second,xx,yy);
   {

   if (small>dd)
   small=dd;
   }
   }
   return small;
  }

   int main()
  {
   int t,n=1;
  int k=0;
  double r,l;
  //Enter the value of t
   scanf("%d",&t);
  while (t--)
  {
  scanf("%d",&n);// Enter the value of n
  vector <pair <int, int> > vg(n);

   for (int i=0; i<n; i++)
   {
   scanf("%i %i",&vg[i].first,&vg[i].second);  

   //Enter the value of x and y co-odinates

   }
   for (int i=0; i<n; i++)
    {
   r=radius(vg,i,n);
    l= (round(r*100.00))/100.00;


    printf("%g\t%i\n",l);
    }  
    }
    return 0;  
     /* Error: wrong number of template arguments (1, should be 2)|
     provided for ‘template<class _T1, class _T2> struct        
      std::pair’|*/ 
  }

【问题讨论】:

  • 请缩进你的代码

标签: c++ templates vector stl std-pair


【解决方案1】:

在您的函数声明中,删除 vector 模板类型周围的括号:

double radius ( vector<pair<int, int> > vk, int ii, int n)

一些注意事项:

  • 始终检查 scanf 的返回值
  • 避免通过值传递vectors
  • 首选 C++ iostream 而非 C 样式 printfs/scanf
  • 用内联函数替换 COMP
  • 正确缩进你的代码

【讨论】:

  • 成功了!!删除一对括号是解决方案!!!!它在早期的代码中工作,所以我尝试再次按值传递它。我使用了 C 风格的 printfs/scanf,因为它减少了花费的时间。我肯定会实施最后两条建议。谢谢你
【解决方案2】:

应该是std::vector&lt;std::pair&lt;int, int&gt; &gt;,虽然我不明白你为什么要通过值而不是通过 const 引用来传递它。

【讨论】:

  • 我已经包含了“使用命名空间标准;”因此我没有在它前面加上 std :: 。我也尝试在以前的程序中按值传递它,它给了我想要的输出。
【解决方案3】:

我看到以下问题:

一个

using namespace std

缺少;。应该是

using namespace std;

两个

double radius ( vector<pair<(int, int)> > vk, int ii, int n)

应该是

double radius ( vector<pair<int, int> > vk, int ii, int n)

三个

printf("%g\t%i\n",l);

您有两个格式说明符,但只有一个值被传递给函数。

【讨论】:

  • 该;在原始代码中。谢谢你,我删除了括号并编译了我的代码。在 printf 部分,错误也不存在于原始代码中
  • @KrisnaTaapad 那么你为什么不发布你的原始代码呢?你的编辑器不支持复制粘贴吗?
  • 这是一个很长的程序,所以我不得不对其进行一些修改,以便在此处仅发布相关部分。错误地,我没有修改那些部分。
猜你喜欢
  • 2017-10-10
  • 2012-10-21
  • 2016-08-18
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 2018-12-04
  • 1970-01-01
  • 2012-08-28
相关资源
最近更新 更多