Constructor for '' must explicitly initialize the reference member ''

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!