简体   繁体   English

将向量成员(结构)传递给函数

[英]passing a vector member (struct) to a function

I am getting errors on following lines of code.I am trying to pass vector p's element p[0],p[1],p[2],p[3] to the function distance. 我在下面的代码行中遇到错误。我试图将向量p的元素p [0],p [1],p [2],p [3]传递给函数距离。

typedef struct {
     long long x,y;
} point; 

long long distance (point A,point B)
{
    int d1 = A.x - B.x ;
    int d2 = A.y - B.y ;
    long long d = d1 * d1 + d2 *d2 ;
    return d ;
}

 //in main function I declared vector <point> p and took input and then,
    x1 = distance (p[0],p[1]) ; // this line is causing error
    x2 = distance (p[1],p[2]) ; // this line is causing error
    x3 = distance (p[2],p[3]) ; // this line is causing error
    x4 = distance (p[3],p[0]) ; // this line is causing error

errors generated : 产生的错误:

In file included from /usr/include/c++/4.6/bits/stl_algobase.h:66:0,
                 from /usr/include/c++/4.6/bits/char_traits.h:41,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from d.cpp:19:
/usr/include/c++/4.6/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<point>’:
d.cpp:83:27:   instantiated from here
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:166:53: error: no type named ‘iterator_category’ in ‘struct point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:167:53: error: no type named ‘value_type’ in ‘struct point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:168:53: error: no type named ‘difference_type’ in ‘struct point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:169:53: error: no type named ‘pointer’ in ‘struct point’
/usr/include/c++/4.6/bits/stl_iterator_base_types.h:170:53: error: no type named ‘reference’ in ‘struct point’

Help is needed. 需要帮助。

It's most likely because you have 最可能是因为你有

using namespace std;

in your code. 在您的代码中。 This makes the compiler think you are referencing std::distance instead of your function. 这使编译器认为您是在引用std::distance而不是函数。

The obvious solution, and what I really recommend, is to stop with using namespace std; 一个明显的解决方案,也是我的真正建议,是停止using namespace std; in your code. 在您的代码中。 In the meantime you could try calling your function using the global scope, like 同时,您可以尝试使用全局范围调用函数,例如

x1 = ::distance (p[0],p[1]);

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

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