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.