从“const int *”到“int *”的无效转换 [英] invalid conversion from ‘const int*’ to ‘int*’
问题描述
我收到以下错误
$ g ++ test.cpp
test.cpp:在函数'int test1 (const int **,int)':
test.cpp:11:14:错误:从'const int *'到'int *'无效转换[-fpermissive]
a = v [i] ;
^
test.cpp:在函数'int main()':
test.cpp:31:20:error:从'int **'到'const int ** '[-fpermissive]
cout<<< test1(c,2)<<< endl;
^
test.cpp:4:5:error:初始化'int test1(const int **,int)'的参数1 [-fpermissive]
int test1 v,int num)
^
b
$ b
#include< iostream>
using namespace std;
int test1(const int ** v,int num)
{
int * a;
int result = 0;
// do somethinghings ....
for(int i = 0; i {
a = v [i]
// do somethings ....
result + = * a;
}
return result;
}
void test2(const int num)
{
cout<< num<< endl;
}
int main()
{
int a = 5;
int b = 8;
int ** c;
c = new int * [2];
c [0] =& a;
c [1] =& b;
cout<<< test1(c,2)<< endl;
test2(a);
delete [] c;
return 0;
}
我给予 int
to test2它要求 const int
,它是确定。但是test1不接受 int **
而不是 const int **
。
在上述代码中,即使typecast不工作:
a =(int *)v [i] ;
AFAIK,const表示我保证不会更改 v
和我did not。
<$ p $ p>
int const * a; // or const int * a;这是相同的。
...然后const正确性将被保留。编译器抱怨,因为您尝试将 v [i]
( int const *
)分配给 int *
,通过它可以改变 v
承诺的元素不会被改变。因为你以后不试图这样做,只需使用 int const *
来保证编译器的安全。
注意, a
将保留一个指针变量(所以你可以重新赋值),只有它会指向整数常量(你不能改变 a
)。要声明一个常量指针,你可以写
int * const a; //指针常量为int变量,或
int const * const a; //指针常量为int常量
另一个错误在原点类似,很难看出为什么它被禁止(因为你只添加 const
,不要试图把它拿走)。考虑:从 int **
到 int const **
的赋值允许,你可以写下面的代码:
int const data [] = {1,2,3,4} //这不应该改变。
int * space;
int ** p =& space;
int const ** p2 = p; //这是不允许的。如果允许,则:
* p2 = data;
** p = 2; //这会写入数据。
这将是坏的,mkay。如果改为写
int test1(const int * const * v,int num)
现在 v
是指向常量的指针(s)。因为 * v
然后是常数,所以漏洞被关闭,编译器会接受它。
I receive the following error
$ g++ test.cpp
test.cpp: In function ‘int test1(const int**, int)’:
test.cpp:11:14: error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive]
a=v[i];
^
test.cpp: In function ‘int main()’:
test.cpp:31:20: error: invalid conversion from ‘int**’ to ‘const int**’ [-fpermissive]
cout<<test1(c,2)<<endl;
^
test.cpp:4:5: error: initializing argument 1 of ‘int test1(const int**, int)’ [-fpermissive]
int test1(const int **v,int num)
^
when compiling the following code:
#include <iostream>
using namespace std;
int test1(const int **v,int num)
{
int *a;
int result=0;
// do somethings ....
for(int i=0;i<num;i++)
{
a=v[i];
// do somethings ....
result+=*a;
}
return result;
}
void test2(const int num)
{
cout<<num<<endl;
}
int main()
{
int a =5;
int b =8;
int **c;
c=new int *[2];
c[0]=&a;
c[1]=&b;
cout<<test1(c,2)<<endl;
test2(a);
delete [] c;
return 0;
}
i give an int
to test2 which asks for const int
and it is ok. however test1 does not accept int **
instead of const int **
.
in the above code even typecast does not work:
a=(int *)v[i];
AFAIK, const means that I promise that I will not change the value of v
and i didnt. however, the compiler gives me error.
Just write
int const *a; // or const int *a; which is the same.
...then const correctness will be preserved. The compiler complains because you try to assign v[i]
, which is an int const *
, to int *
, through which the elements that v
promised would not be changed could be changed. Since you don't attempt to do that later, just use an int const*
to reassure the compiler.
Note that a
will remain a pointer variable (so you will be able to reassign it), only it will point to integer constants (which you cannot then change through a
). To declare a constant pointer, you would write
int *const a; // pointer constant to int variable,or
int const *const a; // pointer constant to int constant
The other error is similar in origin, although it is a bit more difficult to see why it is forbidden (since you're only adding const
and don't try to take it away). Consider: Were an assignment from int**
to int const **
allowed, you could write the following piece of code:
int const data[] = { 1, 2, 3, 4 }; // this is not supposed to be changed.
int *space;
int **p = &space;
int const **p2 = p; // this is not allowed. Were it allowed, then:
*p2 = data;
**p = 2; // this would write to data.
And that would be bad, mkay. If you instead write
int test1(const int *const *v, int num)
Now v
is a pointer (variable) to pointer constant(s) to int constant(s). Since *v
is then constant, the loophole is closed, and the compiler will accept it.
这篇关于从“const int *”到“int *”的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!