No Match for Operator[]

2019-09-15 18:52发布

问题:

So I'm trying to use a sorting function (similar to bubble) and pass into it an object. If that object is bigger (alphabetically) then switch then return true and switch that with the before it. I keep getting an error though inside the if statement inside mySort() which says "no match for operator[] in arr[j]" but from my understanding I'm passing an object array right? Why is this happening and how can I solve it?

Here's the driver

#include <iostream>
#include <fstream>
#include <string>
#include "phoneEntry.h"
using namespace std;

void mySort(PhoneEntry &arr, int size)
{
    bool inOrder = false;
    string temp;
    for (int i = size - 1; i > 0 && !inOrder; i--)
    {
        inOrder = true;
        for (int j = 0; j < i; j++)
        {
            if(arr.alphaGreater(arr[j]))
            {
                inOrder = false;
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
};

int main()
{   
    const int MAXNUM = 500;
    PhoneEntry entry[MAXNUM];
    ifstream filezilla;
    filezilla.open("phone.txt");
    int count = 0;

    if(filezilla)
    {
        while(count < MAXNUM && entry[count].readEntry(filezilla))
        {
            count++;
            mySort(entry[count], count);
        }   

        for(int i = 0; i < count; i++)
        {
            entry[i].writeEntry(cout) << endl;
        }
    }
    else
    {
        cout << "404" << endl;
    }

    return 0;
}

Phone Entry Header

Phone Number Header

Sorting Text (http://pastebin.com/HE8Rsmbg)

回答1:

  1. arr should be an array, not a reference, like this PhoneEntry arr[]

  2. You should be passing an entire array to the sort, not a single element, like this: mySort(entry, count);

Other than this, your code appears OK.

I should add that this is not a C++ - ish solution: the preferred way of managing arrays in C++ is through using the std::vector<T> container from the standard library. The nice thing about vectors is that you do not need to pass their size "on the side".



回答2:

You can use
pointer notation - mySort(PhoneEntry * arr, int size)
or array notation - mySort(PhoneEntry arr[], int size).

If you want to pass the whole array when you call the function, just do mySort(entry, count).



回答3:

arr is not an array in your method.

change your method signature to

void mySort(PhoneEntry *arr, int size)

and call your method with

mySort(entry[count], count);



回答4:

from my understanding I'm passing an object array right?

No, you are not passing an object array. You are passing a reference (indicated by the & in the function header) to the PhoneEntry element that is at the count-th position in the entry array. You probably meant PhoneEntry* arr in the header of mySort -- that would require a pointer to a PhoneEntry instance, and since the name of an array can be interpreted as a pointer to the first element of that array, you could simply pass entry as the first argument to mySort.



回答5:

Substitute this:

void mySort(PhoneEntry * arr, int size)

Instead of this:

// Wrong
mySort(entry[count], count);

... do one of these (as appropriate):

// Always passes the start of the array, "entry[0]":
 mySort(entry, count);

// Passes a pointer to a particular entry,  onwards:
 mySort(&entry[count], count);