C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why can't the source compile?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
sword
Guest





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Why can't the source compile? Reply with 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
Mark P
Guest





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Re: Why can't the source compile? Reply with quote



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





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Re: Why can't the source compile? Reply with quote



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





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Re: Why can't the source compile? Reply with quote

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





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Re: Why can't the source compile? Reply with quote

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





PostPosted: Wed Aug 23, 2006 9:10 am    Post subject: Re: Why can't the source compile? Reply with quote

But if I write the function like this:

void test(int* a);


It will be OK.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.