[英]array of pointers to member functions
I am having a problem with this code because compiler doesn't allows me to run code because says that我对这段代码有问题,因为编译器不允许我运行代码,因为说
cannot convert 'void (Menu::*)(int)' to 'void (Menu::*)(int*)' in assignment.
And I know that this program can be executed with normal functions.而且我知道这个程序可以用普通函数执行。 I've read something of typedef
but I have not been programming for a while and my English is not native.我读过一些typedef
东西,但我已经有一段时间没有编程了,而且我的英语不是母语。 Code has some words in spanish but doesn't affects the code, I can translate if you want but I think it's easy to understand.代码中有一些西班牙语单词,但不影响代码,如果您愿意,我可以翻译,但我认为它很容易理解。 I will appreciate it if somebody can help me.如果有人可以帮助我,我将不胜感激。
#include "Menu.h"
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
void (Menu::*fptr[3])( int*);
typedef void (Menu::*funcion0)( int &)
fptr[0] = &Menu::funcion0; //problem
fptr[1] = &Menu::funcion1; //problem
fptr[2] = &Menu::funcion2; //problem
//void (*f[ 3 ])( int ) = { &Menu::funcion0, &Menu::funcion1, &Menu::funcion2 };
int opcion;
cout << "Escriba un numero entre 0 y 2, 3 para terminar: ";
cin >> opcion;
while ( ( opcion >= 0 ) && ( opcion < 3 ) )
{
//(*f[ opcion ])( opcion );
cout << "Escriba un numero entre 0 y 2, 3 para terminar: ";
cin >> opcion;
}
cout << "Se completo la ejecucion del programa." << endl;
return 0;
}
Menu.h header file Menu.h 头文件
#ifndef MENU_H_INCLUDED
#define MENU_H_INCLUDED
class Menu
{
public:
Menu();
void funcion0( int );
void funcion1( int );
void funcion2( int );
};
#endif // MENU_H_INCLUDED
Menu.cpp菜单.cpp
#include "Menu.h"
#include <iostream>
using std::cout;
using std::cin;
Menu::Menu()
{
}
void Menu::funcion0( int a )
{
cout << "Usted escribio " << a << " por lo que se llamo a la funcion0\n\n";
}
void Menu::funcion1( int b )
{
cout << "Usted escribio " << b << " por lo que se llamo a la funcion1\n\n";
}
void Menu::funcion2( int c )
{
cout << "Usted escribio " << c << " por lo que se llamo a la funcion2\n\n";
}
Your member functions take int
as argument, but you make array of pointers to member functions as if arguments were pointer of type int*
.您的成员函数将int
作为参数,但您将指向成员函数的指针数组视为参数是int*
类型的指针。 Pointer to function arguments and return type must be exactly same as function's that is pointed to.指向函数参数和返回类型的指针必须与所指向的函数完全相同。 Change:改变:
void (Menu::*fptr[3])( int*);
to:到:
void (Menu::*fptr[3])(int);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.