C++ 编译器错误:从“int”到“int*”的无效转换 [-fpermissive]|
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9167834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiler error: invalid conversion from 'int' to 'int*' [-fpermissive]|
提问by Andres29
I got a compiler error:
我收到编译器错误:
main.cpp|59|error: invalid conversion from 'int' to 'int*' [-fpermissive]|
main.cpp|59|错误:从“int”到“int*”的无效转换[-fpermissive]|
The offending line is
违规行是
int *pComienzo = vector, *pFinal = vector[nElementos-1];
Why there is an error? Can someone help me?
为什么会出现错误?有人能帮我吗?
Below is my code:
下面是我的代码:
#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;
}
}
}
}
}
I'm learning...
我在学...
回答by templatetypedef
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
这样做的原因是它vector
是一个int*
,但是vector[nElementos - 1]
是一个常规的int
。因此声明
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.
试图将最后一个索引处的整数值分配给vector
指针pFinal
,因此编译器错误。
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
它pFinal
指向 的最后一个元素vector
,或
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?
也就是说,既然您使用的是 C++,为什么不使用提供的std::vector
类型并完全避免使用指针呢?
Hope this helps!
希望这可以帮助!
回答by Dan F
vector
is a pointer, but subscripting it as vector[nElementos-1]
dereferences it to simply an int. What it looks like you want is instead
vector
是一个指针,但将其下标为将其vector[nElementos-1]
解引用为简单的 int。看起来你想要的是
int *pComienzo = vector, *pFinal = &(vector[nElementos-1]);
回答by Platinum Azure
An array access/subscript (i.e., a[i]
where a
is an array/pointer and i
is an integer) is an expression with the type being the thing you have in the array.
数组访问/下标(即,a[i]
其中a
是数组/指针并且i
是整数)是一个表达式,其类型是数组中的内容。
Simply use the address-of operator &
to store the address in the pointer:
只需使用 address-of 运算符&
将地址存储在指针中:
int *pComienzo = vector, *pFinal = &vector[nElementos-1];
回答by Krizz
You forgot &
. vector[nElementos-1]
is the value, while you need an address for pFinal
.
你忘了&
。vector[nElementos-1]
是值,而您需要pFinal
.
Either *pFinal = &(vector[nElementos-1])
or *pFinal = vector + nElementos-1
.
无论是*pFinal = &(vector[nElementos-1])
或*pFinal = vector + nElementos-1
。