0
//Menu.h

#include<iostream>
#include<conio.h>
#include <map>
#include <string>
#include <functional>
#include <utility>

using namespace std;
map<string,function< void() > > mapa;
string names[100];
string functions[100];
char keys[100];
int pos=0;

void menu(string name,char key,string functionc)
{
    names[pos]=name;
    keys[pos]=key;
    functions[pos]=functionc;
    mapa.insert(map<string,function< void()> >::value_type(functionc,functionc));
    pos++;
}

void write()
{
    for(int i=0;i<pos;i++)
    {
        cout<<names[pos]<<" ";
        cout<<endl;
    }
}

错误:错误 1 ​​错误 C2064:术语不计算为采用 0 个参数的函数

//Main.cpp
    #include <iostream>
    #include <map>
    #include <string>

    #include"Menu.h"
    using namespace std;

    void ime()
    {
    cout<<"k";
    }

    int main() {
    menu("ime1",'c',"ime");
    pisi();

    system("PAUSE");
      return 0;
    }   

我想让标题通用,以便用户可以制作菜单。它将计算出它的名称以及需要按下哪个字符才能访问它的功能用户将使其成为自己的功能,然后从它需要使用它的标题中......

4

4 回答 4

4

C++ doesn't support this. You'd need a programming language property called reflection.

Use a scripting language instead.

于 2013-07-04T11:10:20.147 回答
1

I can make your code compile by changing it thus:

#include <iostream>
#include <conio.h>
#include <map>
#include <string>
#include <functional>
#include <utility>

using namespace std;
map<string,string > mapa;
string names[100];
string functions[100];
char keys[100];
int pos=0;
void menu(string name,char key,string functionc)
{

    names[pos]=name;
    keys[pos]=key;
    functions[pos]=functionc;
    mapa.insert(std::make_pair(functionc,functionc));
    pos++;
}
void write()
{
    for(int i=0;i<pos;i++)
    {
        cout<<names[pos]<<" ";

        cout<<endl;
    }
}

I am worried by the menu.h comment at the top - is this a header file? Now, that has sorted the compile error, but probably doesn't do what you want. Do you want a map of names to functions?

std::map<std::string, std::function<void()>> strings_to_functions;

With a function taking no parameters and returning void, for example

void fn1()
{
    std::cout << "fn1\n";
}

I can now add it to the map,

strings_to_functions.insert(std::make_pair("fn1", fn1));

and call it

strings_to_functions["fn1"]();
于 2013-07-04T11:10:52.353 回答
1

你正试图std::function<void()>std::string这里做一个:

map<string,function< void()> >::value_type(functionc,functionc)
        //                                           ^^^^^^^^^

你需要传递一些不带参数的可调用的东西,返回void. 例如:

void foo() {};

mapa.insert(std::make_pair(functionc, foo));
于 2013-07-04T10:57:53.803 回答
0
//MENU.h
#include<iostream>
#include<conio.h>
#include <map>
#include <string>
#include <functional>
#include <utility>

using namespace std;
map<string,function< void() > > mapa;
string names[100];
string functions[100];
char keys[100];
int pos=0;

void menu(string name,char key, function< void() >functionc)
{
    names[pos]=name;
    keys[pos]=key;
    functions[pos]=functionc;
    mapa.insert(std::make_pair(name,functionc));
    pos++;
}

//Main.cpp
    #include <iostream>
    #include <map>
    #include <string>

    #include"Menu.h"
    using namespace std;

    void ime()
    {
    cout<<"k";
    }

    int main() {
    menu("ime1",'c', ime);
    pisi();

    system("PAUSE");`enter code here`
      return 0;
    } 
于 2017-01-03T14:41:06.760 回答