Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.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++新手。我想了解对象指针和指向成员函数的指针。我编写了以下代码: pcc.cpp: In function ‘int main()’: pcc.cpp:15: error: cannot declare pointer to ‘void’ member pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object pcc.cpp:18: error: ‘t’ cannot be used as a function._C++_Member Function Pointers - Fatal编程技术网

c++;成员函数指针问题 我是C++新手。我想了解对象指针和指向成员函数的指针。我编写了以下代码: pcc.cpp: In function ‘int main()’: pcc.cpp:15: error: cannot declare pointer to ‘void’ member pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object pcc.cpp:18: error: ‘t’ cannot be used as a function.

c++;成员函数指针问题 我是C++新手。我想了解对象指针和指向成员函数的指针。我编写了以下代码: pcc.cpp: In function ‘int main()’: pcc.cpp:15: error: cannot declare pointer to ‘void’ member pcc.cpp:15: error: cannot call member function ‘void golu::man()’ without object pcc.cpp:18: error: ‘t’ cannot be used as a function.,c++,member-function-pointers,C++,Member Function Pointers,代码: #include <iostream> using namespace std; class golu { int i; public: void man() { cout<<"\ntry to learn \n"; } }; int main() { golu m, *n; void golu:: *t =&golu::man(); //making pointer to member function

代码:

#include <iostream>
using namespace std;
class golu
{
   int i;
public:
   void man()
   {
      cout<<"\ntry to learn \n";
   }
};
int main()
{
   golu m, *n;
   void golu:: *t =&golu::man(); //making pointer to member function

   n=&m;//confused is it object pointer
   n->*t();
}
我的问题如下:

  • 我在这个代码中做错了什么
  • 如何制作对象指针
  • 如何制作指向类的成员函数的指针以及如何使用它们

  • 请给我解释一下这些概念。

    'void golu::*t=&golu::man();'应更改为“void(golu::*t)(=&golu::man;”您正在尝试使用指向函数的指针,而不是指向静态函数结果的指针

    “void golu::*t=&golu::man();”应更改为“void(golu::*t)(=&golu::man;”您正在尝试使用指向函数的指针,而不是指向静态函数结果的指针

    (1)函数指针未正确声明

    (2) 你应该这样声明:

    void (golu::*t) () = &golu::man;
    
    void (golu::*t)() = &golu::man;
    
    (3) 成员函数指针应与
    类的对象一起使用(1)函数指针声明不正确

    (2) 你应该这样声明:

    void (golu::*t) () = &golu::man;
    
    void (golu::*t)() = &golu::man;
    

    (3) 成员函数指针应与
    类的对象一起使用

    此处更正了两个错误:

    int main()
    {
       golu m, *n;
       void (golu::*t)() =&golu::man; 
    
       n=&m;
       (n->*t)();
    }
    
  • 您需要指向函数的指针
  • 运算符的优先级不是您期望的优先级,我必须添加括号<代码>n->*t()
  • 被解释为
    (n->*(t())
    ,而您需要
    (n->*t)(

    此处更正了两个错误:

    int main()
    {
       golu m, *n;
       void (golu::*t)() =&golu::man; 
    
       n=&m;
       (n->*t)();
    }
    
  • 您需要指向函数的指针
  • 运算符的优先级不是您期望的优先级,我必须添加括号<代码>n->*t()被解释为
    (n->*(t())
    ,而您需要
    (n->*t)(

    成员函数指针具有以下形式:

    R (C::*Name)(Args...)
    
    其中,
    R
    是返回类型,
    C
    是类类型,
    Args…
    是函数的任何可能参数(或无)

    有了这些知识,指针应该如下所示:

    void (golu::*t) () = &golu::man;
    
    void (golu::*t)() = &golu::man;
    
    注意成员函数后缺少的
    ()
    。这将尝试调用刚得到的成员函数指针,如果没有对象,这是不可能的。
    现在,通过一个简单的typedef,它变得更具可读性:

    typedef void (golu::*golu_memfun)();
    golu_memfun t = &golu::man;
    
    最后,使用成员函数不需要指向对象的指针,但需要括号:

    golu m;
    typedef void (golu::*golu_memfun)();
    golu_memfun t = &golu::man;
    (m.*t)();
    

    括号很重要,因为
    ()
    运算符(函数调用)的优先级(也称为)高于
    *
    (和
    ->*
    )运算符。

    成员函数指针具有以下形式:

    R (C::*Name)(Args...)
    
    其中,
    R
    是返回类型,
    C
    是类类型,
    Args…
    是函数的任何可能参数(或无)

    有了这些知识,指针应该如下所示:

    void (golu::*t) () = &golu::man;
    
    void (golu::*t)() = &golu::man;
    
    注意成员函数后缺少的
    ()
    。这将尝试调用刚得到的成员函数指针,如果没有对象,这是不可能的。
    现在,通过一个简单的typedef,它变得更具可读性:

    typedef void (golu::*golu_memfun)();
    golu_memfun t = &golu::man;
    
    最后,使用成员函数不需要指向对象的指针,但需要括号:

    golu m;
    typedef void (golu::*golu_memfun)();
    golu_memfun t = &golu::man;
    (m.*t)();
    

    括号很重要,因为
    ()
    运算符(函数调用)的优先级(也称为)高于
    *
    (和
    ->*
    )运算符。

    您能解释一下优先级吗?在这方面我不理解优先权的概念。修改后的答案适合你吗?这就是你认为1 + 2×3是7而不是9的原因。看,你能解释一下关于普里奥蒂的事吗?在这方面我不理解优先权的概念。修改后的答案适合你吗?这就是你认为1 + 2×3是7而不是9的原因。看见