如何将这个std::array传递给一个函数?

时间:2022-09-28 22:56:20
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.

在这两个语句中,我都得到了相同的错误:没有合适的std转换::array * currentpath to LINE。为什么会这样呢?我应该用另一种方式传递数组吗?我也尝试过传递currentPaths作为引用,但是它告诉我类型的引用不能初始化。 ,10u>

2 个解决方案

#1


4  

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> &currentPaths);

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:

最后,第一个元素是0。在进行数组访问时,首选使用下标操作符[]。所以:

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;
}

#2


0  

You are dereferencing (currentPaths+1) which is of type std::array* (more precisely: you are incrementing the pointer and then accessing its pointed data) while you probably want to retrieve the first element of currentPaths, that is: currentPaths[0] (the first index in an array is 0).

您正在取消引用(currentPaths+1),它的类型是std::array*(更准确地说:您正在增加指针,然后访问它的指向数据),而您可能希望检索当前路径的第一个元素,即:currentPaths[0](数组中的第一个索引是0)。

#1


4  

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> &currentPaths);

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:

最后,第一个元素是0。在进行数组访问时,首选使用下标操作符[]。所以:

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;
}

#2


0  

You are dereferencing (currentPaths+1) which is of type std::array* (more precisely: you are incrementing the pointer and then accessing its pointed data) while you probably want to retrieve the first element of currentPaths, that is: currentPaths[0] (the first index in an array is 0).

您正在取消引用(currentPaths+1),它的类型是std::array*(更准确地说:您正在增加指针,然后访问它的指向数据),而您可能希望检索当前路径的第一个元素,即:currentPaths[0](数组中的第一个索引是0)。