将向量成员(结构)传递给函数
passing a vector member (struct) to a function
我在以下代码行中遇到错误。我试图将向量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
生成的错误:
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’
需要帮助。
这很可能是因为你有
using namespace std;
在您的代码中。这会使编译器认为您引用的是std::distance
,而不是函数。
显而易见的解决方案,也是我真正建议的,是在代码中停止使用using namespace std;
。在此期间,您可以尝试使用全局范围调用您的函数,如
x1 = ::distance (p[0],p[1]);
相关文章:
- 使用不带参数的函数访问结构元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 链表,反向函数,数据结构
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 需要帮助将结构数组传递给函数
- 使用cpp中的结构和函数的多个学生条目
- 如何传递带有通过引用传递的结构参数的函数?
- 在类构造函数中使用结构变量
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在类构造函数中定义结构变量的参数
- 如何在模板类函数中分配结构值?
- 我应该在C++中将这些结构用作参数化构造函数吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 在结构外部调用指向函数的指针
- 函数:结构的数组
- C++错误:只能为对象和函数结构指定存储类
- 将libuv与函数结构一起使用,而不是函数回调
- 从c#调用c++函数-结构体,指针,函数指针
- 函数结构数组输入cin问题