C++错误 C2100:非法间接寻址 - 气泡排序

C++ Error C2100: Illegal Indirection--bubbleSort

本文关键字:气泡 排序 间接寻址 非法 错误 C2100 C++      更新时间:2023-10-16

我正在尝试将一个向量发送到 bubbleSort 函数中,以组织从最大值到最小值的数字,因为它们是逐个生成的,但我收到"C2100:非法间接"警告。有人可以帮我吗?

private: void bubbleSort(vector<int> &matrixPtr)
{
    int temp;             
    int numLength = *matrixPtr.size( );//length of vector 
    for (int i = 1; (i <= numLength);i++)
    {
        for (int j=0; j < (numLength -1); j++)
        {
            if (*matrixPtr[j+1] > *matrixPtr[j])      
            { 
                temp = *matrixPtr[j];//Swap elements
                *matrixPtr[j] = *matrixPtr[j+1];
                *matrixPtr[j+1] = temp;
            }
        }
    }
}

bubbleSort 是从它前面的另一个函数中提取的:

 bubbleSort(&output);//pass to bubble sort
              for (int rows=0;rows<creation->getZeroRows();rows++)
                {
                 for (int cols=0;cols<creation->getCols();cols++)
                 {
                     txt_DisplayRowSum->Text= String::Concat(txt_DisplayRowSum->Text, (*creation->zeroArrayPtr)[rows][cols]," ");
                 }
                 txt_DisplayRowSum->Text+=" n";
              }

提前感谢您的帮助

您错误地使用了引用。

而不是*matrixPtr.size( )您需要 matrixPtr.size() ,并且在函数中的其他任何地方引用matrixPtr时都不需要*。此外,将向量传递给函数时,您应该只传递 output 而不是 &output

您不应该也不能使用指针等引用。虽然相似,但它们在几个重要方面有所不同。我也推荐这个问题来很好地总结这些差异。