【问题标题】:No Match for Operator[]运算符 [] 不匹配
【发布时间】:2012-03-12 04:24:10
【问题描述】:

所以我尝试使用排序功能(类似于气泡)并将对象传递给它。如果该对象更大(按字母顺序),则 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;
}

Phone Entry Header

Phone Number Header

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

【问题讨论】:

  • 不,您传递的是对单个 PhoneEntry 对象的引用
  • entry[count] 是单个 PhoneEntry。函数mySort 不按值获取(制作副本),而是通过引用获取它,这样效率更高,并且还允许您在需要时对其进行修改。

标签: c++ sorting loops bubble-sort


【解决方案1】:
  1. arr 应该是一个数组,而不是一个引用,像这样PhoneEntry arr[]

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

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

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

【讨论】:

  • 谢谢!这解决了我的问题,但显然我还有更多 xD +1
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    arr 不是您方法中的数组。

    将您的方法签名更改为

    void mySort(PhoneEntry *arr, int size)

    并使用

    调用您的方法

    mySort(entry[count], count);

    【讨论】:

      【解决方案4】:

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

      不,您没有传递对象数组。您正在将 reference(由函数头中的 &amp; 指示)传递给位于 entry 数组中 count-th 位置的 PhoneEntry 元素。您可能是指 mySort 的标头中的 PhoneEntry* arr - 这需要一个指向 PhoneEntry 实例的指针,并且由于数组的名称可以解释为指向该数组的第一个元素的指针,您可以只需将entry 作为第一个参数传递给mySort

      【讨论】:

        【解决方案5】:

        替换为:

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

        【讨论】:

          猜你喜欢
          • 2013-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-09
          • 2018-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多