| View previous topic :: View next topic |
| Author |
Message |
sword Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Why can't the source compile? |
|
|
void test(int*& a);
int main()
{
int a[4];
test(a);
return 0;
}
Who can tell me the reason why the source above can't compile?
I use VC++ 6.0.
test.cpp(7) : error C2664: 'test' : cannot convert parameter 1 from
'int [4]' to 'int *& ' |
|
| Back to top |
|
 |
Mark P Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Re: Why can't the source compile? |
|
|
Phlip wrote:
| Quote: | sword wrote:
void test(int*& a);
a is a reference to a pointer. That means you must pass a pointer, for the
reference to seat on.
int a[4];
test(a);
a is not a pointer, it's an array.
|
But it can decay to a pointer. The issue is that when it's viewed as a
pointer, it's a constant pointer (i.e., it must point to the same place
always). And you can't pass "T const" as "T&" since the constant
qualifier is discarded someone may attempt to modify the T&.
Rewrite the declaration of test as:
void test( int* const& a );
and it should compile.
-Mark |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Re: Why can't the source compile? |
|
|
sword wrote:
| Quote: | void test(int*& a);
int main()
{
int a[4];
test(a);
return 0;
}
Who can tell me the reason why the source above can't compile?
|
You can pass an array of int to a function with int* as its parameter,
but there isn't an automatic conversion from an array of int to an int
pointer reference.
--
Ian Collins. |
|
| Back to top |
|
 |
Isold.Wang@gmail.com Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Re: Why can't the source compile? |
|
|
void test(int* a);
int main()
{
int a[4];
test(a);
return 0;
}
sword wrote:
| Quote: | void test(int*& a);
int main()
{
int a[4];
test(a);
return 0;
}
Who can tell me the reason why the source above can't compile?
I use VC++ 6.0.
test.cpp(7) : error C2664: 'test' : cannot convert parameter 1 from
'int [4]' to 'int *& ' |
|
|
| Back to top |
|
 |
Phlip Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Re: Why can't the source compile? |
|
|
sword wrote:
| Quote: | void test(int*& a);
|
a is a reference to a pointer. That means you must pass a pointer, for the
reference to seat on.
| Quote: | int a[4];
test(a);
|
a is not a pointer, it's an array.
Either take out the & (most likely what you need), or introduce a pointer:
int * p = a;
test(p);
And you might want to read ahead in your tutorial, to soak this stuff in...
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!! |
|
| Back to top |
|
 |
sword Guest
|
Posted: Wed Aug 23, 2006 9:10 am Post subject: Re: Why can't the source compile? |
|
|
But if I write the function like this:
void test(int* a);
It will be OK. |
|
| Back to top |
|
 |
|