无法将函数指针作为该类的另一个成员函数的参数传递给该类的成员函数

问题描述 投票:0回答:1

我需要帮助... 在注释中提出了适当的问题。程序的编译器错误和警告为零!!我担心使用函数指针从另一个成员函数调用成员函数。 (确切地说,setMatrixto()试图使用函数指针调用setElement()函数)

加上不知何故,“ hello there”没有打印到控制台。我期望它显示为输出。也许setMatrixto()根本没有被调用!

头文件定义

#ifndef MATRIXOPERATIONS_H
#define MATRIXOPERATIONS_H

class MatrixOperations;

typedef int (MatrixOperations::*INTFUNC)(int,int);
typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);

class MatrixOperations
{
    public:
        MatrixOperations();
        MatrixOperations(int size);
        ~MatrixOperations();

        //diagonal matrix funtions
        void displayMatrixOf(INTFUNC f);
        void setMatrixTo(VOIDFUNC f);

        int getElement(INTFUNC from, int i, int j);
        void setElement(VOIDFUNC to,int i ,int j, int value);

        int fromDiagonalArray(int i, int j);
        void toDiagonalArray(int i, int j, int value);
    protected:

    private:
        int size;
        int* a;

};



#endif // MATRIXOPERATIONS_H

CPP实施文件

#include "MatrixOperations.h"
#include <iostream>
using namespace std;


MatrixOperations::MatrixOperations()
{
    //ctor
    size = 3;
    a = new int[size];



}
MatrixOperations::MatrixOperations(int size)
{
    //ctor
    this->size = size;
    a = new int[size];


}


MatrixOperations::~MatrixOperations()
{
    //dtor
    delete[] a;
}




///////////////////FUCNTION POINTER SECTION///////////////////////////////////
int MatrixOperations::getElement(INTFUNC from, int i, int j)
{
    return (this->*from)(i,j);

}

void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{

    (this->*to)(i,j,value);
}




/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
int MatrixOperations::fromDiagonalArray(int i, int j)
{
    if(i==j)
    {
        return a[i];
    }
    else
    {
        return 0;

    }
}
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
    a[i] = value;
}

///////////////////////////////////////////////////////////////////
void MatrixOperations::displayMatrixOf(INTFUNC f)
{
    for(int i{0}; i < size; i++)
    {
        for(int j{0}; j < size; j++)
        {
            cout << getElement(f,i,j) << "\t"; //is this the correct way to send the function pointer?
        }
        cout << endl;
    }

}

void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
    cout << "Hello there!!";                      //not getting this output.. whats wrong??
    for(int i{0}; i < size; i++)
    {


            int value {};
            cout << "Enter value diagonal element " << i << " : ";
            cin >> value;
            setElement(f,i,i,value);             //is this the correct way to send the function pointer?

    }

}
///////////////////////////////////////////////////////////////////////////////

主文件

#include <iostream>
#include "MatrixOperations.h"

typedef MatrixOperations MATRIX;


using namespace std;

int main()
{
    MATRIX m1;

    m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a "Hello there!" but i am not getting that output either
    return 0;
}

EDIT2:我在一个文件中添加了所有类定义和主要功能。出奇!!这有效。我很困惑?? !!!

#include <iostream>

using namespace std;

class MatrixOperations;

typedef void (MatrixOperations::*VOIDFUNC)(int,int,int);
typedef MatrixOperations MATRIX;

class MatrixOperations
{
    public:
        MatrixOperations();
        MatrixOperations(int size);
        ~MatrixOperations();


        //diagonal matrix funtions

        void setMatrixTo(VOIDFUNC f);
        void setElement(VOIDFUNC to,int i ,int j, int value);
        void toDiagonalArray(int i, int j, int value);
    private:
        int size;
        int* a;

};
MatrixOperations::MatrixOperations()
{    //ctor
    size = 3;
    a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{    //ctor
    this->size = size;
    a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
    //dtor
    delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{

    (this->*to)(i,j,value);
}




/////////////////////////////////DIAGONAL ARRAY OPERATIONS/////////////////////////////////////////////////
void MatrixOperations::toDiagonalArray(int i, int j, int value)
{
    a[i] = value;
}

///////////////////////////////////////////////////////////////////
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
    cout << "Hello there!!" << endl;
    for(int i{0}; i < size; i++)
    {


            int value {};
            cout << "Enter value diagonal element " << i << " : ";
            cin >> value;
            setElement(f,i,i,value);

    }

}

int main()
{

    MATRIX m1;

    m1.setMatrixTo(MATRIX::toDiagonalArray);
    return 0;
}
c++ function-pointers member-function-pointers
1个回答
0
投票

在两种情况下,代码都没有错。它只是我的调试器未在管理员模式下运行。我收到了错误代码740。因此我以管理员模式启动了IDE,瞧瞧它能起作用。

© www.soinside.com 2019 - 2024. All rights reserved.