簡體   English   中英

指向成員函數的指針數組

[英]array of pointers to member functions

我對這段代碼有問題,因為編譯器不允許我運行代碼,因為說

cannot convert 'void (Menu::*)(int)' to 'void (Menu::*)(int*)' in assignment. 

而且我知道這個程序可以用普通函數執行。 我讀過一些typedef東西,但我已經有一段時間沒有編程了,而且我的英語不是母語。 代碼中有一些西班牙語單詞,但不影響代碼,如果您願意,我可以翻譯,但我認為它很容易理解。 如果有人可以幫助我,我將不勝感激。

#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 頭文件

#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

菜單.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";
}

您的成員函數將int作為參數,但您將指向成員函數的指針數組視為參數是int*類型的指針。 指向函數參數和返回類型的指針必須與所指向的函數完全相同。 改變:

void (Menu::*fptr[3])( int*);

到:

void (Menu::*fptr[3])(int);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM