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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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++ 错误:无法转换'&书信电报;未解析的重载函数类型>';至';函数指针'; #包括 #包括 #包括 #包括 #包括 #包括 #包括 b类{ 公众: b(); 无效ef(无效(*f)(); }; d类:公共b类{ 公众: d(); 作废打印(); }; b::b(){} void b::ef(void(*f)(){f();} d::d():b(){} void d::print(){cout_C++_Oop_Member Function Pointers - Fatal编程技术网

C++ 错误:无法转换'&书信电报;未解析的重载函数类型>';至';函数指针'; #包括 #包括 #包括 #包括 #包括 #包括 #包括 b类{ 公众: b(); 无效ef(无效(*f)(); }; d类:公共b类{ 公众: d(); 作废打印(); }; b::b(){} void b::ef(void(*f)(){f();} d::d():b(){} void d::print(){cout

C++ 错误:无法转换'&书信电报;未解析的重载函数类型>';至';函数指针'; #包括 #包括 #包括 #包括 #包括 #包括 #包括 b类{ 公众: b(); 无效ef(无效(*f)(); }; d类:公共b类{ 公众: d(); 作废打印(); }; b::b(){} void b::ef(void(*f)(){f();} d::d():b(){} void d::print(){cout,c++,oop,member-function-pointers,C++,Oop,Member Function Pointers,这是您的代码,已修复。但我可以看到它有许多“错误”,即“做您要求的,但不是您(可能)想要的。” print方法需要作用于d对象。但这意味着基类必须知道派生类。这很尴尬。如果基类有一个virtual print函数,那么它可以传入该函数,并调用该虚拟函数的派生类重写。但我们不是这样做的在这儿吃点东西 #include<iostream> #include<bitset> #include<string.h> #include<string> #inc

这是您的代码,已修复。但我可以看到它有许多“错误”,即“做您要求的,但不是您(可能)想要的。”

print
方法需要作用于
d
对象。但这意味着基类必须知道派生类。这很尴尬。如果基类有一个
virtual print
函数,那么它可以传入该函数,并调用该虚拟函数的派生类重写。但我们不是这样做的在这儿吃点东西

#include<iostream>
#include<bitset>
#include<string.h>
#include<string>
#include<vector>
#include<math.h>
#include<stdarg.h>


class b {
public:
    b();
    void ef(void(*f)());
};

class d : public b{
public:
    d();
    void print();
};

b::b() {}
void b::ef(void(*f)()) { f(); }

d::d(): b(){}
void d::print() { cout<<"WORKS"<<endl; }

int main() {
    d obj;
    obj.ef(obj.print);
}
#包括
使用std::cout;
名称空间{
d类;
b类{
公众:
b();
无效ef(d&,无效d:*)();
};
d类:公共b类{
公众:
d();
作废打印();
};
b::b(){}
void b::ef(d&dobj,void(d::*f)(){
(dobj.*f)();
}
d::d():b(){}
void d::print(){

您是否可以将成员函数指向
&d::print
。但是成员函数有一个隐藏参数:指向对象的指针。因此函数指针签名不匹配。指向非成员函数的指针与指向成员函数的指针不兼容。不同之处在于,可以调用指针而不调用非成员函数一个对象,但指向成员函数的指针需要一个对象。传递
obj.print
并不仅仅传递对象,而是传递函数(您确实需要使用操作符
&
的地址)。我建议您查看模板或,以及。
#include <iostream>

using std::cout;

namespace {

class d;

class b {
public:
    b();
    void ef(d&, void(d::*)());
};

class d : public b {
public:
    d();
    void print();
};

b::b() {}

void b::ef(d& dobj, void(d::*f)()) {
    (dobj.*f)();
}

d::d() : b() {}

void d::print() {
    cout << "WORKS\n";
}

} // anon

int main() {
    d obj;
    obj.ef(obj, &d::print);
}