3

Currently doing a project for school, and the assignment I got was to turn a C algorithm into C++ (I started learning C++ about 10 hours ago, when I got the assignment).

Doing a function I got the error: [Error] invalid conversion from 'long int' to 'long int*' [-fpermissive]

Here I create a global variable that's used in the testInstance function

    static long *c;                         // total capacity

This is the function that has the error.

    void testInstance (Exitem **f, Exitem **l, int n, int r, int type, int v){

        Exitem *a;

        *f = a;
        *l = &a[n-1];

        c = maketest(*f, *l, type, r, v);

    }

This is the function maketest (called on the line that gives me the error).

    long maketest (Exitem *F, Exitem *L, int type, int r, int v){

        register Exitem *J;
        register long sum;
        long c;
        short r1;

        sum = 0;
        r1 = r / 10;

        for (J = F; J <= L; J++){
            J->w = (longRand() % (r));

            switch (type){
                case 1:
                    J->p = (longRand() % (r)) + 1;
                    break;

                case 2:
                    J->p = (longRand() % (2*r1+1)) + J->w - r1;
                    if (J->p <= 0)
                        J->p = 1;
                    break;

                case 3:
                    J->p = J->w + 10;
                    break;

                case 4:
                    J->p = J->w;
                    break;  
            }

            sum += J->w;
        }
        c = sum / 2;
        return c;

    }

My question is: What the hell am I doing wrong? How can I fix that?

Carlos Ramírez
  • 121
  • 1
  • 3
  • 9

2 Answers2

3

The return value of maketest() is long. You write that long into c, which is of type pointer to long.

That is the mismatch.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

You have declared:

static long *c;

but your function does:

long maketest (Exitem *F, Exitem *L, int type, int r, int v)
{
    ...
    return c;
}

which causes the mismatch, since the return type is long, while c is a pointer to long.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • While you have spotted another mistake, it would be warned as 'long int*' to 'long int', wouldn't it? I.e. the other way around. – Yunnosch Jun 25 '17 at 21:19
  • @Yunnosch I see your answer, aren't we saying the same thing? +1 – gsamaras Jun 25 '17 at 21:27
  • We both spotted problems - and OP is now aware, too - and grateful. Excuse the nitpicking, but I think the problem you spotted is not the one which causes the exact warning OP mentioned in the title. I expect yours would be the next warning, or the previous one. 'long->long*' is not 'long* ->long'. +1 – Yunnosch Jun 25 '17 at 21:30