std::array<LINE,10> currentPaths=PossibleStrtPaths();
LINE s=shortestLine(currentPaths); //ERROR
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;
}
At both those statements I'm getting the same error: no suitable conversion from std::array<LINE,10U>*currentPaths to LINE
. Why is this so? Should I pass the array another way? I've also tried passing currentPaths as a reference, but it tells me that a reference of the type cannot be initialized.
You said you tried a reference and it failed. I don't know why, because that was the correct thing to do.
LINE CShortestPathFinderView::shortestLine(std::array<LINE,10> ¤tPaths);
From the sounds of it, you also used a reference for the temporary variable. That's wrong.
std::array<LINE,10>& currentPaths = PossibleStrtPaths(); // WRONG
std::array<LINE,10> currentPaths = PossibleStrtPaths(); // RIGHT
LINE s = shortestLine(currentPaths);
And finally, the first element is number zero. The subscripting operator []
is preferred when you are doing array access. So:
LINE s = currentPaths[0];
But you also can easily get the first item from the iterator.
Final code:
/* 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;
}
您正在取消引用(currentPaths+1)
,其类型为std::array*
(更准确地说:您是在递增指针,然后访问其指向的数据),而您可能想检索currentPaths
的第一个元素,即: currentPaths[0]
(数组中的第一个索引为0)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.