Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:06:33+00:00 2026-05-27T07:06:33+00:00

I try to compile this answer related to how to store functional objects with

  • 0

I try to compile this answer related to how to store functional objects with difrent signature in a container (eg: std::map) I compeated (seems to me) provided answer code:

#include <functional>
#include <iostream>
#include <string>
#include <map>

class api {
    //maps containing the different function pointers
    std::map<std::string, void(*)()> voida;
    std::map<std::string, int(*)(std::string, const int&)> stringcrint;
    friend class apitemp;
public:
    //api temp class 
    //given an api and a name, it converts to a function pointer  
    //depending on parameters used
    class apitemp {
        const std::string* n;
        api* p;
    public:
        apitemp(const std::string* name, const api* parent) 
            : n(name), p(parent) {}
        operator void(*)()() 
        {return p->void[*n];}
        operator int(*)(std::string, const int&)() 
        {return p->stringcrint[*n];}
    }; 
    //insertion of new functions into appropriate maps
    void insert(std::string name, void(*ptr)()) 
    {voida[name]=ptr;}
    void insert(std::string name, int(*ptr)(std::string, const int&))
    {stringcrint[name]=ptr;}
    //operator[] for the name gets halfway to the right function
    apitemp operator[](std::string n) const {return apitemp(n, this);}
} myMap;

int hello_world(std::string name, const int & number )
{
    name += "!";
    std::cout << "Hello, " << name << std::endl;
    return number;
}

int main() {
    myMap.insert("my_method_hello", &hello_world ); 
    //  int a = myMap["my_method_hello"]("Tim", 25);
}

But I get 12 strange errors on lines with operators:

Error 14  error C2665: 'api::apitemp::apitemp' : none of the 2 overloads could convert all the argument types 
Error 4   error C2586: incorrect user-defined conversion syntax : illegal indirections
Error 8   error C2586: incorrect user-defined conversion syntax : illegal indirections
Error 9   error C2440: 'initializing' : cannot convert from 'const api *' to 'api *'  
Error 10  error C2439: 'api::apitemp::p' : member could not be initialized    
Error 13  error C2232: '->api::stringcrint' : left operand has '' type, use '.'   
Error 2   error C2091: function returns function  
Error 3   error C2091: function returns function  
Error 6   error C2091: function returns function  
Error 7   error C2091: function returns function  
Error 11  error C2059: syntax error : '[' 
Error 1   error C2059: syntax error : '*'
Error 5   error C2059: syntax error : '*'
Error 12  error C2039: 'p' : is not a member of 'api'

So I wonder – how to make it compile?

Update: After fixes (thanks to hvd’s answer ) I we got this:

#include <boost/function.hpp>
#include <iostream>
#include <string>
#include <map>

template <typename T> struct identity { typedef T type; };
class api {
    //maps containing the different function pointers
    std::map<std::string, identity<void(*)()>::type > voida;
    std::map<std::string, identity<int(*)(std::string, const int&)>::type > stringcrint;
    friend class apitemp;
public:
    //api temp class 
    //given an api and a name, it converts to a function pointer  
    //depending on parameters used
    class apitemp {
        std::string* n;
        api* p;
    public:
        apitemp(std::string* name, api* parent) 
            : n(name), p(parent) {}
        operator identity<void(*)()>::type()
        {return p->voida[*n];}
        operator identity<int(std::string, const int&)>::type*()
        {return p->stringcrint[*n];}
    }; 
    //insertion of new functions into appropriate maps
    void insert(std::string name, void(*ptr)()) 
    {voida[name]=ptr;}
    void insert(std::string name, int(*ptr)(std::string, const int&))
    {stringcrint[name]=ptr;}
    //operator[] for the name gets halfway to the right function
    apitemp operator[](std::string n) {return apitemp(n, this);}
} myMap;

int hello_world(std::string name, const int & number )
{
    name += "!";
    std::cout << "Hello, " << name << std::endl;
    return number;
}

int main() {
    myMap.insert("my_method_hello", &hello_world ); 
        int a = myMap["my_method_hello"]("Tim", 25);
}

Yet one error stell stands:

Error 1   error C2665: 'api::apitemp::apitemp' : none of the 2 overloads could convert all the argument types
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:06:34+00:00Added an answer on May 27, 2026 at 7:06 am

    Since I wrote the orgional code for you I feel obliged to fix it 🙁

    #include <functional>
    #include <iostream>
    #include <string>
    #include <map>
    
    class api {
        //maps containing the different function pointers
        typedef void(*voidfuncptr)();
        typedef int(*stringcrintptr)(std::string, const int&);
    
        std::map<std::string, voidfuncptr> voida;
        std::map<std::string, stringcrintptr> stringcrint;
    public:
        //api temp class 
        //given an api and a name, it converts to a function pointer  
        //depending on parameters used
        class apitemp {
            const std::string n;
            const api* p;
        public:
            apitemp(const std::string& name, const api* parent) 
                : n(name), p(parent) {}
            operator voidfuncptr() 
            {return p->voida.find(n)->second;}
            operator stringcrintptr() 
            {return p->stringcrint.find(n)->second;}
        }; 
        //insertion of new functions into appropriate maps
        void insert(const std::string& name, voidfuncptr ptr) 
        {voida[name]=ptr;}
        void insert(const std::string& name, stringcrintptr ptr)
        {stringcrint[name]=ptr;}
        //operator[] for the name gets halfway to the right function
        apitemp operator[](std::string n) const {return apitemp(n, this);}
    } myMap;
    
    int hello_world(std::string name, const int & number )
    {
        name += "!";
        std::cout << "Hello, " << name << std::endl;
        return number;
    }
    
    int main() {
        myMap.insert("my_method_hello", &hello_world ); 
        int a = myMap["my_method_hello"]("Tim", 25);
    }
    

    http://ideone.com/SXAPu

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to compile this code: struct BasicVertexProperties { Vect3Df position; }; struct
When I try to compile this: import java.awt.* ; class obj { public static
When I try to compile this: public static Rand searchCount (int[] x) { int
When i try to compile this: public static int compareCardhl (Card c1, Card c2)
I'm getting a nothing to repeat error when I try to compile this: search
When I try to compile TOSSIM in tiny OS v-2.0.2 , this is the
I receive PatternSyntaxException when try to compile the following regex: bd.matches((a)?b(?(1)c|d)) this regex matches
This compiles: class Ex1 { public int show() { try { int a=10/10; return
I try to compile code, that beggins with: #include<stdlib.h> #include<GL/gl.h> #include<glaux.h> with command: cc
I try to compile and link my application in 2 steps : Compiling: g++

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.