g++ help
Ian McCulloch
ianmcc at vaneyk.lorentz.leidenuniv.nl
Wed Apr 3 01:49:34 EST 2002
On Tue, 2 Apr 2002, Alfred wrote:
> I am at a loose end here :)
> I am getting this error from g++:
> ../Src/SteamValidateUserIDTickets.cpp:1533: no matching function for call to
> `ExtractNext (u8 *,
> unsigned int, u8 *&, u8 *&)'
> ../../../Common/Network/SocketHelperFunctions.h:408: candidates are: void
> common::ExtractNext (u8 *,
> const u8 *&, const u8 *)
> ../../../Common/Network/SocketHelperFunctions.h:419: void
> common::ExtractNext (u16 *,
> const u8 *&, const u8 *)
> ../../../Common/Network/SocketHelperFunctions.h:437: void
> common::ExtractNext (u32 *,
> const u8 *&, const u8 *)
> ../../../Common/Network/SocketHelperFunctions.h:459: void
> common::ExtractNext (u8 *,
> unsigned int, const u8 *&, const u8 *)
>
> Why, oh why, can't g++ (2.96) match the function call to the last function?
> Surely it can do the type conversion. What am I doing/thinking wrong?
>
> Sincerly,
> Confused ;)
>
> Alfred
>
Stripping it down a bit, you are trying to call a function as
func(u8*&)
ie, the parameter is a reference to a pointer to a u8, and you expect it
to match the definition
func(const u8*& arg)
ie, the argument is a reference to a pointer to a (const u8)
This is not allowed, it would violate const-correctness. To see why,
consider a sample implementation of func():
const u8 my_const_u8 = 10; // global constant
void func(const u8*& arg)
{
arg = &my_const_u8; // fine
}
int main()
{
u8* foo;
func(foo); // not allowed, but pretend it was
*foo = 20; // *** modifies my_const_u8
}
Check whether ExtractNext really needs to modify the pointer. If it
doesn't, change the signature so that you pass the pointer by value or
const reference (that would look like const u8* const&). If you do need
to modify the pointer then you have to decide once and for all whether it
is a pointer-to-const or a pointer-to-non-const, and be consistent with it
whenever you are passing the pointer by reference.
Avoid at all costs using a const_cast, it will just come back and bite you
sooner or later.
BTW, C++ (maybe C too?) prohibits conversion from a T** to a const T**,
for exactly the same reason, just substitute another pointer for the
reference.
HTH,
Ian McCulloch
More information about the linux
mailing list