Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 需要一些初始化成员函数指针数组的帮助吗_C++ - Fatal编程技术网

C++ 需要一些初始化成员函数指针数组的帮助吗

C++ 需要一些初始化成员函数指针数组的帮助吗,c++,C++,我对编程非常陌生&在我正在读的一本书中遇到一个程序。我在其中发现了一个编译错误 错误表示“可变大小的对象‘ptrFunc’可能未初始化”。(它指向数组的末尾) 请告知,有什么问题。提前谢谢 #include<iostream> using namespace std; class cDog { public: void speak() const { cout<<"\nWoof W

我对编程非常陌生&在我正在读的一本书中遇到一个程序。我在其中发现了一个编译错误

错误表示“可变大小的对象‘ptrFunc’可能未初始化”。(它指向数组的末尾)

请告知,有什么问题。提前谢谢

#include<iostream>

using namespace std;

class cDog
{
    public:
            void speak() const
            {
                cout<<"\nWoof Woof!!";
            }
            void move() const
            {
                cout<<"\nWalking to heel...";
            }
            void eat() const
            {
                cout<<"\nGobbling food...";
            }
            void growl() const
            {
                cout<<"\nGrrrgh...";
            }
            void whimper() const
            {
                cout<<"\nWhinig noises...";
            }
            void rollOver() const
            {
                cout<<"\nRolling over...";
            }
            void playDead() const
            {
                cout<<"\nIs this the end of little Ceaser?";
            }
};

int printMenu();

int main()
{
    int selection = 0;
    bool quit = 0;
    int noOfFunc = 7;
    void (cDog::*ptrFunc[noOfFunc])() const = {

    &cDog::eat,
    &cDog::growl,
    &cDog::move,
    &cDog::playDead,
    &cDog::rollOver,
    &cDog::speak,
    &cDog::whimper
    };

    while(!quit)
    {
        selection = printMenu();

        if(selection == 8)
        {
            cout<<"\nExiting program.";
            break;
        }
        else
        {
            cDog *ptrDog = new cDog;
            (ptrDog->*ptrFunc[selection-1])();
            delete ptrDog;
        }
    }
    cout<<endl;

    return 0;
}

int printMenu()
{
    int sel = 0;

    cout<<"\n\t\tMenu";
    cout<<"\n\n1. Eat";
    cout<<"\n2. Growl";
    cout<<"\n3. Move";
    cout<<"\n4. Play dead";
    cout<<"\n5. Roll over";
    cout<<"\n6. Speak";
    cout<<"\n7. Whimper";
    cout<<"\n8. Quit";
    cout<<"\n\n\tEnter your selection : ";
    cin>>sel;

    return sel;
}
#包括
使用名称空间std;
类cDog
{
公众:
void speak()常量
{
库特
noOfFunc
不是const限定的;您需要将其声明为
const int
才能将其用作数组大小(数组大小必须在编译时已知)

但是,当您在此处声明类似于初始值设定项的数组时,可以忽略大小;编译器将根据初始值设定项中的元素数确定大小。您可以简单地说:

void (cDog::*ptrFunc[])() const = {
`改成

void (cDog::*ptrFunc[7])() const = 

void (cDog::*ptrFunc[7])() const = 
void (cDog::*ptrFunc[])() const =