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

huangapple go评论89阅读模式
英文:

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

问题

我需要帮助... 适当的问题已在评论中提出。 程序没有编译错误和警告!!我担心在一个成员函数中使用函数指针调用另一个成员函数。(确切地说,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();

    //对角线矩阵函数
    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;
}

///////////////////函数指针部分///////////////////////////////////
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);
}

///////////////////////////////对角线数组操作/////////////////////////////////////////////////
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"; //这是发送函数指针的正确方式吗?
        }
        cout << endl;
    }
}

void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
    cout << "Hello there!!"; //没有得到这个输出..出了什么问题??
    for (int i{0}; i < size; i++)
    {

        int value{};
        cout << "Enter value diagonal element " << i << " : ";
        cin >> value;
        setElement(f, i, i, value); //这是发送函数指针的正确方式吗?

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

主文件

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

typedef MatrixOperations MATRIX;

using namespace std;

int main()
{
    MATRIX m1;

    m1.setMatrixTo(MATRIX::toDiagonalArray); //期望得到 "Hello there!" 但是我也没有得到这个输出
    return 0;
}

编辑2:我将所有类定义和主函数添加到一个单独的文件中。令人惊讶!!这个方法可以工作。我很困惑?!

#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();

    //对角线矩阵函数

    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;
}
英文:

I need help... appropriate questions have been asked in the comments. The programs has zero compiler errors and warnings!! I have concerns with calling a member function from another member function using function pointers. (To be precise, setMatrixto() is trying to call setElement() function using function pointer)

Plus somehow the "hello there" is not being printed to the console. I was expecting it to show up as output.Maybe the setMatrixto() is not getting called at all!!

Header File definition

#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 Implementation File

#include &quot;MatrixOperations.h&quot;
#include &lt;iostream&gt;
using namespace std;
MatrixOperations::MatrixOperations()
{
//ctor
size = 3;
a = new int[size];
}
MatrixOperations::MatrixOperations(int size)
{
//ctor
this-&gt;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-&gt;*from)(i,j);
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this-&gt;*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 &lt; size; i++)
{
for(int j{0}; j &lt; size; j++)
{
cout &lt;&lt; getElement(f,i,j) &lt;&lt; &quot;\t&quot;; //is this the correct way to send the function pointer?
}
cout &lt;&lt; endl;
}
}
void MatrixOperations::setMatrixTo(VOIDFUNC f)
{
cout &lt;&lt; &quot;Hello there!!&quot;;                      //not getting this output.. whats wrong??
for(int i{0}; i &lt; size; i++)
{
int value {};
cout &lt;&lt; &quot;Enter value diagonal element &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;;
cin &gt;&gt; value;
setElement(f,i,i,value);             //is this the correct way to send the function pointer?
}
}
///////////////////////////////////////////////////////////////////////////////

Main File

#include &lt;iostream&gt;
#include &quot;MatrixOperations.h&quot;
typedef MatrixOperations MATRIX;
using namespace std;
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray); //was expecting a &quot;Hello there!&quot; but i am not getting that output either
return 0;
}

EDIT2: I added all the class definitions and main function in one single file. SURPRISINGLY!! this works . I am confused??!!!

#include &lt;iostream&gt;
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-&gt;size = size;
a = new int[size];
}
MatrixOperations::~MatrixOperations()
{
//dtor
delete[] a;
}
void MatrixOperations::setElement(VOIDFUNC to,int i ,int j, int value)
{
(this-&gt;*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 &lt;&lt; &quot;Hello there!!&quot; &lt;&lt; endl;
for(int i{0}; i &lt; size; i++)
{
int value {};
cout &lt;&lt; &quot;Enter value diagonal element &quot; &lt;&lt; i &lt;&lt; &quot; : &quot;;
cin &gt;&gt; value;
setElement(f,i,i,value);
}
}
int main()
{
MATRIX m1;
m1.setMatrixTo(MATRIX::toDiagonalArray);
return 0;
}

答案1

得分: 0

在这两种情况下,代码都没有问题。只是我的调试器没有以管理员模式运行。我收到了错误代码740。所以我以管理员模式启动了我的集成开发环境,然后它就可以工作了。

英文:

There is nothing wrong with the code in both cases. Its just my debugger was not running in admin mode. I got error code 740. So I launched my IDE in admin mode and voila it worked.

huangapple
  • 本文由 发表于 2020年1月6日 20:01:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定