From: Chris on
I am reading a file and when I read a certain string I want to execute a
certain function.

This is the code i have but I must be doing something wrong, it doesn't
compile. I am using c++ (GCC) 3.4.6 20060404 on a RHEL 4 workstation
There are three functions in class Convert "void Convert::writeVerts()",
"void Convert::writeCell(int)" and "void Convert::writeBoun ()"

typedef void (Convert::*FUNCPTR )( void ) ;
typedef void (Convert::*FUNCPTR2)( int ) ;
typedef std::map< std::string,FUNCPTR >::value_type MapItem;
typedef std::map< std::string,FUNCPTR2 >::value_type MapItem2;
std::map< std::string, FUNCPTR > FunctionTable;

FunctionTable.insert(MapItem(std::string("$* GRID CARDS"),writeVerts));
FunctionTable.insert(MapItem2(std::string("$* ELEMENT CARDS"),writeCell));
FunctionTable.insert(MapItem(std::string("$* LOAD AND CONSTRAINT CARDS" ) ,
writeBoun ) );

The compile error I get is

error: no matching function for call to `std::pair<const std::string, void
(Convert::*)()>::pair(std::string, <unresolved overloaded function type>)'

candidates are: std::pair<const std::string, void (Convert::*
()>::pair(const std::pair<const std::string, void (Convert::*)()>&)


From: Gianni Mariani on
Chris wrote:
> I am reading a file and when I read a certain string I want to execute a
> certain function.
>
> This is the code i have but I must be doing something wrong, it doesn't
> compile. I am using c++ (GCC) 3.4.6 20060404 on a RHEL 4 workstation
> There are three functions in class Convert "void Convert::writeVerts()",
> "void Convert::writeCell(int)" and "void Convert::writeBoun ()"
>
> typedef void (Convert::*FUNCPTR )( void ) ;
> typedef void (Convert::*FUNCPTR2)( int ) ;
> typedef std::map< std::string,FUNCPTR >::value_type MapItem;
> typedef std::map< std::string,FUNCPTR2 >::value_type MapItem2;
> std::map< std::string, FUNCPTR > FunctionTable;
>

try to change the way you're taking the address of the function.

> FunctionTable.insert(MapItem(std::string("$* GRID CARDS"),writeVerts));


&Convert::writeVerts

> FunctionTable.insert(MapItem2(std::string("$* ELEMENT CARDS"),writeCell));

&Convert::writeCell

> FunctionTable.insert(MapItem(std::string("$* LOAD AND CONSTRAINT CARDS" ) ,
> writeBoun ) );

&Convert::writeBoun


>
> The compile error I get is
>
> error: no matching function for call to `std::pair<const std::string, void
> (Convert::*)()>::pair(std::string, <unresolved overloaded function type>)'
>
> candidates are: std::pair<const std::string, void (Convert::*
> ()>::pair(const std::pair<const std::string, void (Convert::*)()>&)
>
>
From: Chris on
Gianni Mariani wrote:

> Chris wrosuggesteddd am reading a file and when I read a certain string I
want to execute a
>> certain function.
>>
>> This is the code i have but I must be doing something wrong, it doesn't
>> compile. I am using c++ (GCC) 3.4.6 20060404 on a RHEL 4 workstation
>> There are three functions in class Convert "void Convert::writeVerts()",
>> "void Convert::writeCell(int)" and "void Convert::writeBoun ()"
>>
>> typedef void (Convert::*FUNCPTR )( void ) ;
>> typedef void (Convert::*FUNCPTR2)( int ) ;
>> typedef std::map< std::string,FUNCPTR >::value_type MapItem;
>> typedef std::map< std::string,FUNCPTR2 >::value_type MapItem2;
>> std::map< std::string, FUNCPTR > FunctionTable;
>>
>
> try to change the way you're taking the address of the function.
>
>> FunctionTable.insert(MapItem(std::string("$* GRID CARDS"),writeVerts));
>
>
> &Convert::writeVerts
>
>> FunctionTable.insert(MapItem2(std::string("$* ELEMENT
>> CARDS"),writeCell));
>
> &Convert::writeCell
>
>> FunctionTable.insert(MapItem(std::string("$* LOAD AND CONSTRAINT CARDS"
>> ) ,
>> writeBoun ) );
>
> &Convert::writeBoun
>
>
>>
>> The compile error I get is
>>
>> error: no matching function for call to `std::pair<const std::string,
>> void (Convert::*)()>::pair(std::string, <unresolved overloaded function
>> type>)'
>>
>> candidates are: std::pair<const std::string, void (Convert::*
>> ()>::pair(const std::pair<const std::string, void (Convert::*)()>&)
>>
>>
Hi Gianni thank you for the reply.

I have tried to do it as you have suggested and get the following error.

error: no matching function for call to `std::map<std::string, void
(Convert::*)(), std::less<std::string>, std::allocator<std::pair<const
std::string, void (Convert::*)()> > >::map(std::string, <unresolved
overloaded function type>)'

candidates are: std::map<_Key, _Tp, _Compare, _Alloc>::map(const
std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = std::string, _Tp =
void (Convert::*)(), _Compare = std::less<std::string>, _Alloc =
std::allocator<std::pair<const std::string, void (Convert::*)()> >]


From: Ulrich Eckhardt on
Chris wrote:
> This is the code i have but I must be doing something wrong, it doesn't
> compile. I am using c++ (GCC) 3.4.6 20060404 on a RHEL 4 workstation
> There are three functions in class Convert "void Convert::writeVerts()",
> "void Convert::writeCell(int)" and "void Convert::writeBoun ()"
>
> typedef void (Convert::*FUNCPTR )( void ) ;
> typedef void (Convert::*FUNCPTR2)( int ) ;
> typedef std::map< std::string,FUNCPTR >::value_type MapItem;
> typedef std::map< std::string,FUNCPTR2 >::value_type MapItem2;
> std::map< std::string, FUNCPTR > FunctionTable;
>
> FunctionTable.insert(
> MapItem(std::string("$* GRID CARDS"),writeVerts));
> FunctionTable.insert(
> MapItem2(std::string("$* ELEMENT CARDS"),writeCell));
> FunctionTable.insert(
> MapItem(std::string("$* LOAD AND CONSTRAINT CARDS" ) ,
> writeBoun ) );

Okay, one thing should be obvious: you are trying to insert two different
types of objects into the same map. That won't work. Other than that, the
implicit conversion of a function name into a pointer to that function
only applies to non-member functions and only for backward compatibility,
so you have to use the syntax Gianni gave you already.

> The compile error I get is
>
> error: no matching function for call to `std::pair<const std::string,
> void (Convert::*)()>::pair(std::string,
> <unresolved overloaded function type>)'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is the important part of the message: the compiler believes that that
function is overloaded, so it can't determine the correct type from just
taking the function pointer. In order to resolve that, you need to use
static_cast to force the function pointer to the right overload.

Uli

--
FAQ: http://ma.rtij.nl/acllc-c++.FAQ.html
From: Dan Bloomquist on


Chris wrote:

> I am reading a file and when I read a certain string I want to execute a
> certain function.
>
> This is the code i have but I must be doing something wrong, it doesn't
> compile. I am using c++ (GCC) 3.4.6 20060404 on a RHEL 4 workstation
> There are three functions in class Convert "void Convert::writeVerts()",
> "void Convert::writeCell(int)" and "void Convert::writeBoun ()"
>
> typedef void (Convert::*FUNCPTR )( void ) ;

Try:
typedef void ( *FUNCPTR )( void ) ;

and:

> FunctionTable.insert(MapItem( std::string("$* GRID CARDS"), &Convert::writeVerts() ) );

Best, Dan.