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

Unable to pass a function pointer to a member function of the class as an argument to another member function of the same class

本文关键字:函数 成员 另一个 一类 参数传递 指针      更新时间:2023-10-16

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

另外,不知何故,"你好"没有打印到控制台上。我期待它显示为输出。也许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;
}

在这两种情况下,代码都没有错。只是我的调试器没有在管理模式下运行。我收到错误代码 740。所以我在管理模式下启动了我的 IDE,瞧它工作了。