【发布时间】:2015-04-04 16:44:57
【问题描述】:
当尝试对存储在结构数组中的库存进行冒泡排序时,我在编译以下代码时遇到两个不同的错误:
void SORT_INVENTORY(Books* list, int max, int position)
{
bool swap;
string temp;
do
{
swap = false;
for (int count = 0 ; count < (position - 1) ; count++)
{
if ( tolower(list[count].Title) > tolower(list[count + 1].Title))
{
temp = list[count];
list[count] = list[count + 1];
list[count + 1] = temp;
swap = true;
}
}
} while (swap);
我希望使用 tolower 来比较两个结构数组的 Title 元素。但是,编译器不会让我运行程序,因为它说 没有匹配的函数来调用 tolower。
当我将 if 语句切换到这个时:
if ( ::tolower(list[count].Title) > ::tolower(list[count + 1].Title))
“无匹配函数”消息消失,但被新消息取代:没有从“字符串”(又名“基本字符串,分配器>”)到“整数”的可行转换。
最后,我收到一条关于 if 语句正文中的语句的一致错误消息,指出在temp = list[count] 和list[count + 1] = temp 中没有可行的重载'='。
最后一个细节:list 是一个声明为结构数据类型的数组。我做错了什么?
【问题讨论】:
标签: c++ arrays sorting struct tolower