I’m new with vectors. I’m trying to add objects to a vector. But the program can’t compile because I have a problem in the code. But I don’t know what is it. The error is:
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Line (void)' to 'Line &&'
The code is:
Line help_line ();
cin >> ln_quan;
vector <Line> figure_line;
for (int i = 0 ; i < ln_quan ; i++)
{
figure_line.push_back(help_line);
}
The compiler says that the error is at the 6-th line (figure_line.push_back(help_line);).
I gave up trying to find a tutorial explaining how to add objects (I give up easily when doing such things…).
And what does ‘Line (void)’ and ‘Line &&’ mean? Is ‘Line (void)’ the class ‘Line’? If so, what does ‘(void)’ mean in this case?
This does not mean “
help_lineshall be an instance ofLinecreated with the default constructor”. It means “help_lineshall be a function, implemented somewhere else, that takes no arguments and returns aLineinstance”.The thing you want is spelled
Line help_line;, with no parentheses.So, you get the following error message:
Line &&is the kind of parameter thatpush_backis expecting. The&&doesn’t really matter here; it’s best thought of, for beginners, as a kind of calling convention. You’re still just passing aLine, because that’s the kind of thing you collect in a vector ofLines.Line(void)is “the type of functions that take no arguments and return aLineinstance”.(void)is another way to write(), for function arguments (it is discouraged in new code, but sometimes needed when interacting with very old C code).