开发者

compiler error: operator "*"and"+" cannot be applied

i asked like this question yesterday but i didnt get good answer about it

in the code below why i cant use * and + in last line?and whats the way to solve that?thanks

    private void bigzarb(int u,int v)
    {
        double n;
        int x=0;
        int y;
        int w=0;
        int z;
        string[] i = textBox7.Text.Split(',');
        int[] nums = new int[i.Length];
        for (int counter = 0; counter < i.Length; counter++)
        {
            nums[counter] = Convert.ToInt32(i[counter]);
        }

        u = nums[0];
        double firstdigits =Math.Floor(Math.Log10(u) + 1);
         v = nums[1];
        double seconddigits = Math.Floor(Math.Log10(v) + 1);
        if (firstdigits >= seconddigits)
        {
            n = firstdigits;

        }
        else
        {
            n = seconddigits;

        }
        if (u == 0 || v == 0)
        {
            MessageBox.Show("the Multiply is 0");
        }
        string threshold = textBox9.Text;
        int intthreshold = Convert.ToInt开发者_如何学编程32(threshold);
        int intn = Convert.ToInt32(n);
        if (intn <= intthreshold)
        {

            double uv = u * v;
            string struv = uv.ToString();
            MessageBox.Show(struv);

        }
        else
        {
           int m  =Convert.ToInt32(Math.Floor(n / 2));

            x = u % 10 ^ m;
            y = u / 10 ^ m;
            w = v % 10 ^ m;
            z = v / 10 ^ m;

            bigzarb(x, w) *( 10 ^ m) +(bigzarb(x,w)+bigzarb(w,y))*10^m +bigzarb(y,z);///compiler gives error operator "*"and"+" cannot be applied to operands of type'void'and'int'

///and compiler gives error operator "*"and"+" cannot be applied to operands of type 'void' and 'void'
        }


    }


Well, there are a few things wrong here:

  • You're trying to use the results of calling a method, but that method has no return value... so there's no result to multiply. This is what the compiler is complaining about.
  • I suspect you think that ^ performs a "power" operation - it's actually a bitwise xor
  • Your final statement doesn't actually do anything with the results of the calculation

Given that your method doesn't return anything, what values do you think are going to be used in an expression such as bigzarb(x, w) *( 10 ^ m)?


Jon Skeet has answered this question, but I though I would be more explicit to explain exactly what is happening on this line...

bigzarb(x, w) *( 10 ^ m) + (bigzarb(x,w)+bigzarb(w,y))*10^m +bigzarb(y,z);

Let's break it into sections

The first statement is

bigzarb(x, w)

But as you'll see from Jon's answer - you are not returning a value from this method...

private void bigzarb(int u,int v)

Now let's replace that bit with it's actual value now we've explained it:

[void] *( 10 ^ m) + (bigzarb(x,w)+bigzarb(w,y))*10^m +bigzarb(y,z);

The same goes for all of the other calls to bigzarb - so let's replace those too...

[void] * ( 10 ^ m) + ([void] + [void]) * 10 ^ m + [void];

So your problem is, in order to use mathematical operators, you need numbers on each side - but you don't have numbers because your method is void.

You could change your method to return a number - but be aware of recursion... when you call this method, it calls itself three times and each of those calls will result in a further three calls to the method. Not good!


Your method bigzarb has a void signature, which you are using in a calulation.


this is because

rerturn type of function gzarb(x, w) is void so you can't do any operatin on a void pointer.

and in the last line ,you are using output of gzarb in a runing calculation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜