Search code examples
c++sortingbubble-sort

Error request for member '.......' in '......', which is of non-class type '.....'


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;
        }
    }
}

how to fix error request for member 'swap' in 'bor.tes::n[i]', which is of non-class type 'char'?


Solution

  • how to fix error request for member 'swap' in 'bor.tes::n[i]', which is of non-class type 'char'?

    Although it seems not to go with the code presented in the question, the diagnostic is relatively clear. It is complaining about an attempt to invoke a method named swap() on the object designated by bor.tes::n[i]. That is unacceptable because the type of bor.tes::n[i] is char, which not only does not have any methods named swap(), but which, not being a class type, does not have any methods at all.

    You need to come up with another way to perform the operation you want, such as perhaps 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);
    

    or there's always a good, old-fashioned three-way swap using a temporary:

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