简体   繁体   English

使用对向量时无效,错误的数字模板参数错误

[英]Invalid, Wrong number template arguments error while using pair vectors

There are 2 types of errors in the code based on vector > vg(n) which i am unable to rectify 基于向量> vg(n)的代码中有两种错误,我无法纠正

  1. In the last line ie; 在最后一行,即; the return 0 statement has an error saying "wrong number of template arguments (1, should be 2)|and a Line:87 link to the STL library which says "provided for 'template struct std::pair" return 0语句有一个错误,指出“模板参数的数量错误(1,应为2)|”,并且指向STL库的Line:87链接显示为“为模板结构std :: pair提供”
  2. The first line of the function (line 7) says that the template arguments are invalid 函数的第一行(第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’|*/ 
  }

它应该是std::vector<std::pair<int, int> > ,尽管我不明白为什么要通过值而不是const引用传递它。

In your function declaration, remove the parenthesis around the vector template type: 在函数声明中,删除vector模板类型的括号:

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

A few notes : 一些注意事项:

  • Always check the return value of scanf 始终检查scanf的返回值
  • Avoid passing vectors by value around 避免按值传递vectors
  • Prefer C++ iostreams to C-style printfs/scanf 比C风格的printfs / scanf更喜欢C ++ iostream
  • Replace the COMP macro by an inline function 用内联函数替换COMP
  • Properly indent your code 正确缩进您的代码

I see the following problems: 我看到以下问题:

One

using namespace std

There's a missing ; 有一个失踪者; . It should be 它应该是

using namespace std;

Two

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

should be 应该

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

Three

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

You have two format specifiers but only one value is being passed to the function. 您有两个格式说明符,但是只有一个值传递给该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM