因此,在返回正确类型的变量时,我一直遇到此错误。它说了一些效果,“非常量的初始值必须是左值”
谁能帮助我修改代码以正确返回给定索引处的字符?这是一个单链列表项目BTW。
char& MyList::operator[](const int i)
{
if (i > size())
return 'z';
Node* temp = head;
for (unsigned int a = 0; a < i; a++)
{
if (a == i)
return temp->value;
temp = temp->next;
}
}
最佳答案
问题行是:
return 'z';
'z'
计算得出的常量不能用作返回char&
的函数的返回值。快速解决:
char& MyList::operator[](const int i)
{
static char z = 'z';
if (i > size())
return z;
Node* temp = head;
for (unsigned int a = 0; a < i; a++)
{
if (a == i)
return temp->value;
temp = temp->next;
}
}