''的构造函数必须显式初始化引用成员“ [英] Constructor for '' must explicitly initialize the reference member ''
本文介绍了''的构造函数必须显式初始化引用成员“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个类
class CamFeed {
public:
// constructor
CamFeed(ofVideoGrabber& cam);
ofVideoGrabber& cam;
};
这个构造函数:
CamFeed :: CamFeed(ofVideoGrabber& cam){
this-> cam = cam;
}
我在构造函数中遇到此错误:
构造函数
你需要使用构造函数初始化器列表:
解决方案 :CamFeed(ofVideoGrabber& cam):cam(cam){}
这是因为引用必须引用因此不能默认构造。一旦你在构造函数体中,所有的数据成员都被初始化了。你的 this-> cam = cam;
行真的是一个赋值,赋值 cam
无论 this-> cam
是指。
I have this class
class CamFeed {
public:
// constructor
CamFeed(ofVideoGrabber &cam);
ofVideoGrabber &cam;
};
And this constructor:
CamFeed::CamFeed(ofVideoGrabber &cam) {
this->cam = cam;
}
I get this error on the constructor: Constructor for '' must explicitly initialize the reference member ''
What is a good way to get around this?
解决方案
You need to use the constructor initializer list:
CamFeed::CamFeed(ofVideoGrabber& cam) : cam(cam) {}
This is because references must refer to something and therefore cannot be default constructed. Once you are in the constructor body, all your data members have been initialized. Your this->cam = cam;
line would really be an assignment, assigning the value referred to by cam
to whatever this->cam
refers to.
这篇关于''的构造函数必须显式初始化引用成员“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文