Compiler error: invalid conversion from 'int' to 'int*' [-fpermissive]|

前端 未结 4 1446
萌比男神i
萌比男神i 2021-01-15 02:20

I got a compiler error:

main.cpp|59|error: invalid conversion from \'int\' to \'int*\' [-fpermissive]|

The offending line is

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 02:45

    Your error is in this line:

    int *pComienzo = vector, *pFinal = vector[nElementos-1];
    

    The reason for this is that vector is an int*, but vector[nElementos - 1] is a regular int. Thus the declaration

    int *pFinal = vector[nElementos - 1];
    

    is trying to assign the integer value at the last index of vector to the pointer pFinal, hence the compiler error.

    To fix this, you may want to do either

    int *pFinal = &vector[nElementos - 1];
    

    which makes pFinal point to the last element of vector, or

    int *pFinal = vector + (nElementos - 1);
    

    which accomplishes the same thing using pointer arithmetic.

    That said, since you're working in C++, why not use the provided std::vector type and avoid working with pointers altogether?

    Hope this helps!

提交回复
热议问题