C++ 我应该如何将这个std::array传递给函数?
在这两个语句中,我都得到了相同的错误:从std::array*currentPaths到LINE没有合适的转换。为什么会这样?我应该以另一种方式传递数组吗?我还尝试将currentPaths作为引用传递,但它告诉我该类型的引用无法初始化。您正在取消引用std::array*类型的currentPaths+1。更准确地说:您正在递增指针,然后访问其指向的数据,而您可能希望检索currentPaths的第一个元素,也就是说:currentPaths[0]数组中的第一个索引是0。您正在解引用类型为std::array*的currentPaths+1。更准确地说:您正在递增指针,然后访问其指向的数据,而您可能想要检索currentPaths的第一个元素,即:currentPaths[0]数组中的第一个索引是0。您说您尝试了一个引用,但失败了。我不知道为什么,因为那是正确的做法C++ 我应该如何将这个std::array传递给函数?,c++,arrays,function,stl,C++,Arrays,Function,Stl,在这两个语句中,我都得到了相同的错误:从std::array*currentPaths到LINE没有合适的转换。为什么会这样?我应该以另一种方式传递数组吗?我还尝试将currentPaths作为引用传递,但它告诉我该类型的引用无法初始化。您正在取消引用std::array*类型的currentPaths+1。更准确地说:您正在递增指针,然后访问其指向的数据,而您可能希望检索currentPaths的第一个元素,也就是说:currentPaths[0]数组中的第一个索引是0。您正在解引用类型为st
LINE CShortestPathFinderView::shortestLine(std::array<LINE,10> *currentPaths)
{
std::array<LINE,10>::iterator iter;
LINE s=*(currentPaths+1); //ERROR
for(iter=currentPaths->begin()+1;iter<=currentPaths->end();iter++)
{
if(s.cost>iter->cost)
s=*iter;
}
std::remove(currentPaths->begin(),currentPaths->end(),s);
//now s contains the shortest partial path
return s;
}
但是您也可以很容易地从迭代器中获取第一项
最终代码:
LINE s = currentPaths[0];
你说你尝试了一个参考,但失败了。我不知道为什么,因为那是正确的做法
LINE CShortestPathFinderView::shortestLine(std::array<LINE,10> *currentPaths)
{
std::array<LINE,10>::iterator iter;
LINE s=*(currentPaths+1); //ERROR
for(iter=currentPaths->begin()+1;iter<=currentPaths->end();iter++)
{
if(s.cost>iter->cost)
s=*iter;
}
std::remove(currentPaths->begin(),currentPaths->end(),s);
//now s contains the shortest partial path
return s;
}
但是您也可以很容易地从迭代器中获取第一项
最终代码:
LINE s = currentPaths[0];
*电流路径+1;你能做到吗?或者你是说*currentPaths[1]@ahenderson我想把currentPaths的第二行对象分配给s。*currentPaths+1不是与currentPaths[1]相同吗?除非null是一个可能的值,否则通过引用传递是更好的方法。使用您的方法,您可能正在访问与CurrentPath[1]相同的*CurrentPath[10]。请记住,您有一个指向数组而不是数组的指针。*CurrentPath+1;你能做到吗?或者你是说*currentPaths[1]@ahenderson我想把currentPaths的第二行对象分配给s。*currentPaths+1不是与currentPaths[1]相同吗?除非null是一个可能的值,否则通过引用传递是更好的方法。使用您的方法,您可能正在访问与CurrentPath[1]相同的*CurrentPath[10]。请记住,您有一个指向数组而不是数组的指针。我已经插入了您给我的代码,但在s=shortestLinecurrentPaths;行中仍然收到一条错误消息:错误C2664:'CShortestPathFinderView::shortestLine':无法将参数1从'std::tr1::array'转换为'std::tr1::array&'@Ghost:显示整个错误消息,包括属性和大小。我想知道您是否有多个名为LINE的类型。[1>Ty=CShortestPathFinderView::Brains::LINE,1>\u Size=10 1>]1>和1>[1>\u Ty=LINE,1>\u Size=10 1>]大脑是我调用的函数_shortestLine的来源。@Ghost:有两种不同类型的函数名为LINE。大脑的数组::LINE与::LINE的数组不同。@Ben Voigt Ohh,我没看到。谢谢!非常感谢。我已经插入了您给我的代码,但在s=shortestLinecurrentPaths;行仍收到错误消息:错误C2664:'CShortestPathFinderView::shortestLine':无法将参数1从'std::tr1::array'转换为'std::tr1::array&'@Ghost:显示整个错误消息,包括属性和大小。我想知道您是否有多个名为LINE的类型。[1>Ty=CShortestPathFinderView::Brains::LINE,1>\u Size=10 1>]1>和1>[1>\u Ty=LINE,1>\u Size=10 1>]大脑是我调用的函数_shortestLine的来源。@Ghost:有两种不同类型的函数名为LINE。大脑的数组::LINE与::LINE的数组不同。@Ben Voigt Ohh,我没看到。谢谢!非常感谢。
LINE s = currentPaths[0];
/* precondition: currentPaths is not empty */
LINE CShortestPathFinderView::shortestLine(std::array<LINE,10>& currentPaths)
{
std::array<LINE,10>::iterator iter = currentPaths.begin();
LINE s = *(iter++);
for(; iter != currentPaths->end(); ++iter) {
if(s.cost>iter->cost)
s=*iter;
}
std::remove(currentPaths.begin(), currentPaths.end(), s);
//now s contains the shortest partial path
return s;
}