Anton Kizema
Anton Kizema

Reputation: 1092

can not convert parameter 1 from int into &int

Lets imagine I have got functions:

int switcherINT(char &c){
    switch (c){
    case '1': return 1; break;
    case '2': return 2; break;  
    case '3': return 3; break;
    case '4': return 4; break;
    case '5': return 5; break;
    case '6': return 6; break;
    case '7': return 7; break;
    case '8': return 8; break;
    case '9': return 9; break;
    case '0': return 0; break;
    default: return err;
    }
}
char switcherCHAR(int &c){
    switch (c){
    case 1: return '1'; break;
    case 2: return '2'; break;  
    case 3: return '3'; break;
    case 4: return '4'; break;
    case 5: return '5'; break;
    case 6: return '6'; break;
    case 7: return '7'; break;
    case 8: return '8'; break;
    case 9: return '9'; break;
    case 0: return '0'; break;
    default: return errCH;
    }
}

and I am trying to compute nest expression:

c.str[i] = switcherCHAR(switcherINT(pthis->str[pthis->size-i-1])-switcherINT(pb->str[pb->size-i-1])-loc);

where

longMath *pthis(this),*pb(&b);
longMath c;
class longMath{
protected:
    char* str;
    int size;

protected:
........

compiler says: "can not convert parameter 1 from int into &int" Haw can I solve this problem?

Upvotes: 0

Views: 695

Answers (2)

Johannes
Johannes

Reputation: 9

It would be much better to use functions like this:

int switcherINT(const char &c) {
    return (c >= '0' && c <= '9') ? c - '0' : err;
}

char switcherCHAR(const int &c) {
    return (c >= 0 && c <= 9) ? '0' + c : errCH;
}

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

The expression that you've given as an argument to switcherCHAR gives you a temporary int. You cannot pass a temporary as a reference - unless you make the reference const. Just change switcherCHAR to take a const int& (and while you're at it, make switcherINT take a const char&). However, this are very simple types and you're probably better off just taking them by value. So change them to take just int and char.

Nonetheless, your functions are pretty strange. It is very easy to convert between a number x and it's char counterpart just by doing '0' + x. The numerical digit characters are guaranteed to be in consecutive order. So if you take the value of '0' and add, lets say, 5, you will get the value of the character '5'.

Upvotes: 4

Related Questions