gpt4 book ai didi

c++ - 将 vector 成员(结构)传递给函数

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:22 26 4
gpt4 key购买 nike

我在以下代码行中遇到错误。我正在尝试将 vector p 的元素 p[0]、p[1]、p[2]、p[3] 传递给函数 distance。

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]);

关于c++ - 将 vector 成员(结构)传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18327968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com