提问者:小点点

对“…”中的成员“…”提出错误请求,该成员是非类类型“…”[已关闭]


for(i=0;i<x;i++)
{
    for(j=0;j<i;j++)
    {
        if(bor.hasil[i]<bor.hasil[j])
        {
            bor.n[i].swap (bor.hasil[j]);
            
            tmp=bor.hasil[i];
            bor.hasil[i]=bor.hasil[j];
            bor.hasil[j]=tmp;
        }
    }
}

如何修复非类类型“char”的“bor.tes::n[i]”中成员“swap”的错误请求?


共1个答案

匿名用户

如何修复非类类型“char”的“bor.tes::n[i]”中成员“swap”的错误请求?

虽然它似乎不符合问题中呈现的代码,但诊断是相对清晰的。 它抱怨有人试图在bor.tes::n[i]指定的对象上调用名为swap()的方法。 这是不可接受的,因为bor.tes::n[i]的类型是char,它不仅没有任何名为swap()的方法,而且它不是类类型,根本没有任何方法。

您需要想出另一种方法来执行您想要的操作,例如std::swap:

// swap the values of x and y, provided that x is move-assignable and move-constructible
// and that y is swappable
std::swap(x, y);

或者总是有一个很好的,老式的三方交换,使用临时的:

// swap the values of chars x and y:
char tmp = x;
x = y;
y = tmp;