+ 5

push_back() method of C++ Vector is (probably) causing strange error

Hi ! Here is my code: https://code.sololearn.com/cm4K03Hu3YC7/#cpp In my code, I've defined a class Test which has only one member named `num`. It also has two constructors. But, when I tried to create an array of Test objects using C++ Vectors, it gave me the following error: "error: binding 'const Test' to reference of type 'Test&' discards qualifiers" And, to be honest with you, the error statement went over my head. Could someone please help me? EDIT: this code is basically a minimal verifiable version of my project

26th Mar 2019, 5:06 AM
777
777 - avatar
4 Respostas
+ 6
Test(const Test& t): num(t.num){ //cpy-ctor } copy constructor is a class member by default, it has access to private members. ;)
26th Mar 2019, 7:11 AM
AZTECCO
AZTECCO - avatar
+ 4
Test(const Test& t).... Push_back calls copy constructor and its implementation wants const
26th Mar 2019, 5:42 AM
AZTECCO
AZTECCO - avatar
+ 4
@AZTECCO, yes that's what I'd tried. But, the problem is I'm making a project in which I have private members instead of public ones and there I've deployed "getters" and "setters". For example: https://code.sololearn.com/cg1lUu29iIP4/#cpp (modified) Sorry, for the incomplete code. So, now I've a different error here, which I suppose is due to the "setters" on const object. Is it so?
26th Mar 2019, 6:33 AM
777
777 - avatar
+ 1
rv7 @Learn_on_the_Web you should add const modifier for getters to be able use it on const objects. https://code.sololearn.com/c7q6izCv01fi/?ref=app
28th Mar 2019, 11:08 PM
px16
px16 - avatar