编译器错误:从'int'到'int *'的无效转换[-fpermissive] | [英] Compiler error: invalid conversion from 'int' to 'int*' [-fpermissive]|

查看:613
本文介绍了编译器错误:从'int'到'int *'的无效转换[-fpermissive] |的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了编译器错误:

main.cpp | 59 |错误:从'int'到'int *'的无效转换[-fpermissive] |

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

违规行是

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

为什么有错误?有人可以帮我吗?

Why there is an error? Can someone help me?

下面是我的代码:

#include <iostream>
#include <ctype.h>

using namespace std;
const unsigned short int MAX_VAL = 10;

int LongitudCadena(char*);

int BuscarCaracter(char *cadena, char caracter);

void Ordenar(int *vector, int nElementos, bool ascendente);

int main()
{
    char *cadena = "asdasd";

    cout << LongitudCadena(cadena) << endl;

    cout << BuscarCaracter(cadena, 'a') << endl;

    int iArray[] = {5,4,3,2,1};

    Ordenar(iArray, 5, 1);

    cout << iArray << endl;

    return 0;
}

int LongitudCadena(char *cadena)
{
    char *c = cadena;
    for(int i = 0; i < MAX_VAL; i++)
    {
        if (c[i] == 0) break;
        cadena++;
    }

    return  cadena - c;
}

int BuscarCaracter(char * cadena, char caracter)
{
    char *pCadena = cadena;
    for (int i = 0; i < MAX_VAL; i++)
    {
        pCadena++;
        if (toupper(cadena[i]) == toupper(caracter))
        return pCadena- cadena;
    }

    return -1;
}

void Ordenar(int *vector, int nElementos, bool ascendente)
{


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

    if (ascendente)
    {
        for (int i = 0; i < nElementos; i++)
        {
            for (; pComienzo < pFinal; pComienzo++, pFinal--)
            {
                if (*pComienzo > *pFinal)
                {
                    *pComienzo += *pFinal;
                    *pFinal -= *pComienzo;
                    *pComienzo -= *pFinal;
                }
            }
        }
    }
}

我正在学习...

推荐答案

您的错误在此行:

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

这样做的原因是 vector int * ,而 vector [nElementos-1] 是常规的 int.因此,声明

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];

试图将 vector 的最后一个索引处的整数值分配给指针 pFinal ,因此出现编译器错误.

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];

使得 pFinal 指向 vector 的最后一个元素,或者

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

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

使用指针算法完成同样的事情.

which accomplishes the same thing using pointer arithmetic.

也就是说,既然您使用的是C ++,为什么不使用提供的 std :: vector 类型并避免完全使用指针呢?

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

希望这会有所帮助!

这篇关于编译器错误:从'int'到'int *'的无效转换[-fpermissive] |的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆