分拣程序
我正经历着最困难的时期。我什至不再理解错误消息,因为错误消息太多了。我认为我的 alphaGreater() 类函数参数有问题,但谁知道呢。任何人都可以使用 mySort() 中的冒泡排序来按字母顺序排序吗?
#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[j+1].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);
}
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "404" << endl;
}
return 0;
}
对文本进行排序 (http://pastebin.com/HE8Rsmbg)
我的错误...
>> g++ sort.cpp -o PhoneSort.exe
object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned
int)(((unsigned int)j) * 20u)))'
object.cpp:35:29: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _C
harT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, st
d::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'const std::basic_s
tring<char>& {aka const std::basic_string<char>&}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_tra
its<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Al
loc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'const char*'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<cha
r>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> =
std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'char'
object.cpp:37:30: error: no match for 'operator=' in '*(arr + ((((unsigned int)j
) + 1u) * 20u)) = temp'
object.cpp:37:30: note: candidate is:
phoneEntry.h:12:7: note: PhoneEntry& PhoneEntry::operator=(const PhoneEntry&)
phoneEntry.h:12:7: note: no known conversion for argument 1 from 'std::string
{aka std::basic_string<char>}' to 'const PhoneEntry&'
I'm having the hardest time with this. I don't even understand the error messages anymore since there's so many of them. I think there's a problem with my alphaGreater() class function parameters but who knows. Can anybody get this to sort alphabetically using the bubble sort inside mySort()?
#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[j+1].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);
}
for(int i = 0; i < count; i++)
{
entry[i].writeEntry(cout) << endl;
}
}
else
{
cout << "404" << endl;
}
return 0;
}
Sorting Text (http://pastebin.com/HE8Rsmbg)
My Errors...
>> g++ sort.cpp -o PhoneSort.exe
object.cpp: In function 'void mySort(PhoneEntry*, int)':
object.cpp:35:29: error: no match for 'operator=' in 'temp = *(arr + ((unsigned
int)(((unsigned int)j) * 20u)))'
object.cpp:35:29: note: candidates are:
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _C
harT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>, st
d::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:541:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'const std::basic_s
tring<char>& {aka const std::basic_string<char>&}'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_tra
its<char>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Al
loc> = std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:549:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'const char*'
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits
, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<cha
r>, _Alloc = std::allocator<char>, std::basic_string<_CharT, _Traits, _Alloc> =
std::basic_string<char>]
c:\mingw\bin\../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h:560:7: not
e: no known conversion for argument 1 from 'PhoneEntry' to 'char'
object.cpp:37:30: error: no match for 'operator=' in '*(arr + ((((unsigned int)j
) + 1u) * 20u)) = temp'
object.cpp:37:30: note: candidate is:
phoneEntry.h:12:7: note: PhoneEntry& PhoneEntry::operator=(const PhoneEntry&)
phoneEntry.h:12:7: note: no known conversion for argument 1 from 'std::string
{aka std::basic_string<char>}' to 'const PhoneEntry&'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
arr
参数包含一个PhoneEntry
值数组。当您写入temp = arr[j]
时,您尝试从该数组中读取一个条目并将其存储在string
中。您无法将PhoneEntry
分配给string
。更改temp
为可以保存PhoneEntry
值的类型,即PhoneEntry
:更好的是,使用
swap
函数来自
标头。删除temp
变量并将上面的三行替换为以下一行:Your
arr
parameter holds an array ofPhoneEntry
values. You attempt to read an entry from that array and store it in astring
when you writetemp = arr[j]
. You can't assign aPhoneEntry
to astring
. Changetemp
to have a type that can holdPhoneEntry
values, namelyPhoneEntry
:Better yet, use the
swap
function from the<algorithm>
header. Remove thetemp
variable and replace the three lines above with this one:尝试
而不是
但我猜真的。
Try
instead of
But I am guessing really.
因此,您将 PhoneEntry 分配给
temp
。temp
是一个 std::string 而 PhoneEntry 不是 - 因此会出现错误。请注意,
temp
仅在最内层作用域中使用,因此您应该在那里声明它,并在同一行上初始化它。 (一般来说,你不应该在没有初始化的情况下声明局部变量。)我还建议你看看 std::swap 函数。
So, you're assigning a PhoneEntry to
temp
.temp
is a std::string and PhoneEntry is not - hence the error.Note that
temp
is only used in the innermost scope, so you should declare it there, and initialise it on the same line. (In general, you should not declare a local variable without initialising it.)I would also suggest that you might have a look at the std::swap function.