运算符[]不匹配
所以我尝试使用排序函数(类似于冒泡)并向其传递一个对象。如果该对象更大(按字母顺序),则 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;
}
Sorting Text (http://pastebin.com/HE8Rsmbg)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
arr
应该是一个数组,而不是引用,就像这样PhoneEntry arr[]
您应该将整个数组传递给排序,而不是单个元素,如下所示:
mySort(entry, count);< /code>
除此之外,您的代码显示正常。
我应该补充一点,这不是 C++ 风格的解决方案:在 C++ 中管理数组的首选方法是使用标准库中的
std::vector
容器。向量的好处是你不需要“在侧面”传递它们的大小。arr
should be an array, not a reference, like thisPhoneEntry arr[]
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".您可以使用
指针表示法 -
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)
.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);
不,您没有传递对象数组。您将引用(由函数头中的
&
表示)传递给位于count< 处的
PhoneEntry
元素entry
数组中的 /code> 第一个位置。您可能指的是mySort
标头中的PhoneEntry* arr
——这需要一个指向PhoneEntry
实例的指针,并且由于array 可以解释为指向该数组第一个元素的指针,您只需将entry
作为第一个参数传递给mySort
即可。No, you are not passing an object array. You are passing a reference (indicated by the
&
in the function header) to thePhoneEntry
element that is at thecount
-th position in theentry
array. You probably meantPhoneEntry* arr
in the header ofmySort
-- that would require a pointer to aPhoneEntry
instance, and since the name of an array can be interpreted as a pointer to the first element of that array, you could simply passentry
as the first argument tomySort
.替换为:
替换为:
... 执行以下操作之一(视情况而定):
Substitute this:
Instead of this:
... do one of these (as appropriate):