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

前端 未结 1 1821
温柔的废话
温柔的废话 2021-02-05 01:59

I have this class

class CamFeed {
public:
    // constructor
    CamFeed(ofVideoGrabber &cam); 
    ofVideoGrabber &cam;

};

And this c

1条回答
  •  一整个雨季
    2021-02-05 02:54

    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.

    0 讨论(0)
提交回复
热议问题