Error: need explicit cast for function parameter

A

Alex Vinokur

=========================
Windows 2000 Professional
Digital Mars C/C++ 8.36
STLport 4.5.3
=========================

I have got a problem with compilation of the following piece of code
using Digital Mars C/C++ compiler.

There was no problem with this code while using GNU g++ 3.2.

What is wrong?


====== C++ code : BEGIN ======

------ bar.h file : Fragment ------
class Foo
{
[---omitted---]

public :
bool foo (const Foo* const) const;

[---omitted---]
}
-----------------------------------


------ bar.cpp file : Fragment ------
[---omitted---]

mem_fun(&Foo::foo); // Line#255

[---omitted---]
-------------------------------------

====== C++ code : END ========


====== Compilation : BEGIN ======

$ sc -I. -I\dm\stlport\stlport -c bar.cpp
mem_fun(&Foo::foo);

bar.cpp(255) : Error: need explicit cast for function parameter 1 to get
from: bool ( Foo::*member func)(const Foo*const )const
to : bool ( Foo::*member func)(const Foo*)const
mem_fun(&Foo::foo),

====== Compilation : END ========


=====================================
Alex Vinokur
mailto:[email protected]
http://mathforum.org/library/view/10978.html
=====================================
 
I

Ivan Vecerina

Hi Alex,
....
| I have got a problem with compilation of the following piece of code
| using Digital Mars C/C++ compiler.
|
| There was no problem with this code while using GNU g++ 3.2.
|
| What is wrong?
....
| bool foo (const Foo* const) const;
....
| bar.cpp(255) : Error: need explicit cast for function parameter 1 to get
| from: bool ( Foo::*member func)(const Foo*const )const
| to : bool ( Foo::*member func)(const Foo*)const
| mem_fun(&Foo::foo),

This is a bug in the compiler. The second const in the declaration of
function foo is actually NOT part of the function's signature.
It applies to the copy of the pointer parameter used within the body
of the foo function.

In any case, you may want to just remove the second const from the
declaration of foo() -- but not from the definition.
This is the style I would recommend in any case.
See:
http://groups.google.com/[email protected]
http://groups.google.com/[email protected]



However, this might trigger a bug in the compiler again: it might
complain that a function defined as:
bool Foo::foo (const Foo* const) const
{ /*....*/ }
does not match the function declared in class Foo as:
bool foo (const Foo* ) const;
Or worse, this error may be reported at link time only...

According to the C++ standard, these are *identical* declarations.

But if the problem persists, also removing the (2nd) const from the
definition of foo() can be a workaround for the broken compiler.


hth,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top