不确定如何解决“会员参考”错误

时间:2018-11-13 03:23:03

标签: c++

我的main.cpp文件中有问题,程序告诉我QuickSort行和for循环都Member reference base type 'int [11]' is not a structure or union。然后在提示行中显示Adding 'int' to a string does not append to the string and "Use array indexing to silence this warning

下面是我的问题所在的main.cpp文件。

#include <iostream>
#include "QuickSort.h"
using namespace std;

int main() {
     int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
     QuickSort(F, 0, F.length-1);
     for (int i = 0; i<F.length; i++){
       cout << F[i] + " ";
    } 
    return 0;
}

以防万一您需要我的其他代码来解密。 以下是我的QuickSort.h文件:

using namespace std;
class QuickSortRecursion{
    public:
    QuickSortRecursion();
    int Partition (int a[], int low, int high);
    void QuickSort(int a[], int low, int high);
private:
};

下面是我的QuickSort.cpp文件:

QuickSortRecursion::QuickSortRecursion(){
    return;
}
int QuickSortRecursion::Partition(int a[], int low, int high){
    int pivot = high;
    int i = low;
    int j = high;
    while (i<j){
        if (a[i] <= a[pivot]){
             i++;
        }if (a[i] > a[pivot]){
            if ((a[i] > a[pivot]) && (a[j] <= a[pivot])){
                int temp = a[i];
                a[i] = a[j];
                a[j] = temp;
                i++;
            } if (a[j] > a[pivot]){
                j--;
            }
        }
    }
    int temp = a[i];
    a[i] = a[pivot];
    a[pivot] = temp;
    return i;
}

    void QuickSortRecursion::QuickSort(int a[], int low, int high){
    if (low >= high){
        return;
    }
    int split = Partition (a, low, high);
    QuickSort(a, low, split-1);
    QuickSort(a, split+1, high);
}

1 个答案:

答案 0 :(得分:1)

int main() {
     int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
     QuickSort(F, 0, F.length-1);
     for (int i = 0; i<F.length; i++){
       cout << F[i] + " ";
    } 
    return 0;
}

F是一个数组,并且数组没有任何成员,因此没有F.length

解决方案(按个人喜好排序)

使用std::size(如果可用)(C ++ 17中的新增功能)。

int main() {
     int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
     QuickSort(F, 0, std::size(F)-1); 
     for (int i = 0; i<std::size(F); i++){
       cout << F[i] + " ";
    } 
    return 0;
}

使用std::vector而不是原始数组。 std :: array更合适,但我没有一个好的公式可以使

std::array<int> F = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
              ^
              Need to specify array size here, and if you knew that you there 
              wouldn't be a problem.

带有std::vector

的示例
int main() {
     std::vector<int> F = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
     QuickSort(F.data(), 0, F.size()-1); 
     for (int i = 0; i<F.size(); i++){
       cout << F[i] + " ";
    } 
    return 0;
}

使用sizeof来获取长度(以字节为单位),然后将其除以sizeof元素以获取数组中的元素数

int main() {
     int F[] = {12, 2, 16, 30, 8, 28, 4, 10, 20, 6, 18};
     const int length = sizeof(F) / sizeof(F[0]);
     QuickSort(F, 0, length -1); 
     for (int i = 0; i<length ; i++){
       cout << F[i] + " ";
    } 
    return 0;
}

注意事项

如果您有一个不执行任何操作的构造函数,请让编译器生成它。

如果您有一个没有状态的类(成员变量),请考虑将其设为namespace

分区看起来不正确。看来您正在尝试使用Lomuto Partitioning,但是有点麻烦。

相关问题