std::array<LINE,10> currentPaths=PossibleStrtPaths();
LINE s=shortestLine(currentPaths); //ERRORLINE 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;
}在这两条语句中,我得到了相同的错误:no suitable conversion from std::array<LINE,10U>*currentPaths to LINE。为何会这样呢?我应该以另一种方式传递数组吗?我还尝试将currentPaths作为引用传递,但它告诉我不能初始化类型的引用。
发布于 2012-12-28 14:17:10
你说你试过推荐信但失败了。我不知道为什么,因为那是正确的做法。
LINE CShortestPathFinderView::shortestLine(std::array<LINE,10> ¤tPaths);根据它的声音,您还使用了对临时变量的引用。这是错误的。
std::array<LINE,10>& currentPaths = PossibleStrtPaths(); // WRONG
std::array<LINE,10> currentPaths = PossibleStrtPaths(); // RIGHT
LINE s = shortestLine(currentPaths);最后,第一个元素是零。在进行数组访问时,首选订阅运算符[]。所以:
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;
}发布于 2012-12-28 14:15:20
您正在取消引用(currentPaths+1),它的类型为std::array* (更准确地说:您正在递增指针,然后访问其指向的数据),而您可能希望检索currentPaths的第一个元素,即:currentPaths[0] (数组中的第一个索引为0)。
https://stackoverflow.com/questions/14070756
复制相似问题