运营商不匹配[] [英] No Match for Operator[]
问题描述
所以我试图使用排序函数(类似于bubble)并将其传递给一个对象。如果该对象更大(字母顺序),然后切换然后返回true并切换与之前它。我不断得到一个错误,虽然在 mySort()
里面的if语句中说没有匹配的操作符[]在arr [j],但从我的理解,我传递一个对象数组是什么?为什么会发生这种情况,我该如何解决?
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?
以下是驱动程序
#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)
Sorting Text (http://pastebin.com/HE8Rsmbg)
推荐答案
-
arr
应该是一个数组,而不是引用,如PhoneEntry arr []
arr
should be an array, not a reference, like thisPhoneEntry arr[]
你应该将整个数组传递给排序,而不是单个元素,例如: mySort(entry,count);
You should be passing an entire array to the sort, not a single element, like this: mySort(entry, count);
我应该补充一点,这不是C ++ - ish解决方案:在C ++中管理数组的首选方法是通过使用 std :: vector< T>
容器。关于向量的好处是,你不需要传递他们的大小在一边。
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".
这篇关于运营商不匹配[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!