coder6720
coder6720

Reputation: 21

Unsure how to fix "Member reference" error

I am having issues in my main.cpp file where the program is telling me that Member reference base type 'int [11]' is not a structure or union for both my QuickSort line and my for loop too. Then in the cout line it says Adding 'int' to a string does not append to the string and "Use array indexing to silence this warning.

Below is my main.cpp file where I am having my issue.

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

Just in case you need my other code to decipher. Below is my QuickSort.h file:

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

Below is my QuickSort.cpp file:

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

Upvotes: 2

Views: 43

Answers (1)

user4581301
user4581301

Reputation: 33932

In

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 is an array and arrays do not have any members so there is no F.length.

Solutions (in order of personal preference)

Use std::size if it is available (New in 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;
}

Use std::vector instead of a raw array. std::array is more fitting, but I don't have a good formula to make

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.

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

Use sizeof to get the length in bytes and then divide it by the sizeof an element to get the number of elements in the array

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

Side Notes

If you have a constructor that does nothing, let the compiler generate it.

If you have a class with no state (member variables) consider making it a namespace instead.

The partitioning doesn't look right. It looks like you're trying for Lomuto Partitioning, but are a little bit off.

Upvotes: 1

Related Questions