运算符[]不匹配

发布于 2025-01-07 23:12:12 字数 1542 浏览 3 评论 0原文

所以我尝试使用排序函数(类似于冒泡)并向其传递一个对象。如果该对象更大(按字母顺序),则 switch 然后返回 true 并将其与之前的对象切换。尽管在 mySort() 内的 if 语句中我不断收到错误,其中显示“与 arr[j] 中的运算符[]不匹配”,但根据我的理解,我正在传递一个对象数组,对吧?为什么会发生这种情况以及如何解决?

这是驱动程序

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

电话条目标题

电话号码标题

对文本进行排序 (http://pastebin.com/HE8Rsmbg)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

记忆で 2025-01-14 23:12:12
  1. arr 应该是一个数组,而不是引用,就像这样 PhoneEntry arr[]

  2. 您应该将整个数组传递给排序,而不是单个元素,如下所示: mySort(entry, count);< /code>

除此之外,您的代码显示正常。

我应该补充一点,这不是 C++ 风格的解决方案:在 C++ 中管理数组的首选方法是使用标准库中的 std::vector 容器。向量的好处是你不需要“在侧面”传递它们的大小。

  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".

断念 2025-01-14 23:12:12

您可以使用
指针表示法 - mySort(PhoneEntry * arr, int size)
或数组表示法 - mySort(PhoneEntry arr[], int size)

如果您想在调用函数时传递整个数组,只需执行mySort(entry, count)即可。

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).

﹂绝世的画 2025-01-14 23:12:12

arr 不是您方法中的数组。

将您的方法签名更改为

void mySort(PhoneEntry *arr, int size)

并使用

mySort(entry[count], count);调用您的方法

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

独享拥抱 2025-01-14 23:12:12

根据我的理解,我正在传递一个对象数组,对吗?

不,您没有传递对象数组。您将引用(由函数头中的& 表示)传递给位于count< 处的PhoneEntry 元素entry 数组中的 /code> 第一个位置。您可能指的是 mySort 标头中的 PhoneEntry* arr ——这需要一个指向 PhoneEntry 实例的指针,并且由于array 可以解释为指向该数组第一个元素的指针,您只需将 entry 作为第一个参数传递给 mySort 即可。

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.

冰雪之触 2025-01-14 23:12:12

替换为:

void mySort(PhoneEntry * arr, int size)

替换为:

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

... 执行以下操作之一(视情况而定):

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文