简体   繁体   中英

Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*'

So in my program, I am supposed to have the user input a base, up to base 32, and a random decimal value to be converted. So if the user entered 5 and 10, it would have to print to the screen 10 in base 5, 4, 3, and 2. However, this is my first time using dynamic allocation so I'm getting weird errors, and this is one I can't fix. (d stands for decimal value, b stands for base, arrayPtr is my dynamic allocation array)

        while(d != 0 && d>b)
    {
        *arrayPtr = digits[(d%b)];
        d = d/b;
        arrayPtr++;
        n++;
    }

    //print statement to read array backwards to give correct output
    for (int i=n-1; i>=0; i--)
    {
        cout<<"Base "<<i <<": "*(arrayPtr+(i*sizeof(*arrayPtr)))<<" ";    }
    b--;
    cout<<endl;

I am getting an error(invalid operands of types 'const char [3]' and 'int*' to binary 'operator*') on the line that says: cout<<"Base "<(arrayPtr+(isizeof(*arrayPtr)))<<" "; Can someone help me understand why this is happening?

You're missing a << between ": " and *(...) so the compiler things you are trying to multiply ": " by the value of the expression in parens.

//---------------------v
cout<<"Base "<<i <<": "*(arrayPtr+(i*sizeof(*arrayPtr)))<<" ";

should read

cout<<"Base "<<i <<": "<<*(arrayPtr+(i*sizeof(*arrayPtr)))<<" ";
//---------------------^^

this is why people tend to put spaces between operators so that it's easier to spot a mistake like this.

cout<<"Base "<<i<<": "*(arrayPtr+(i*sizeof(*arrayPtr)))<<" ";
//vs
cout << "Base " << i << ": " << *(arrayPtr + (i * sizeof(*arrayPtr))) << " ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM