[英]C++ Error when adding objects to vector
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);). 编译器说错误位于第6行(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? 'Line(void)'和'Line &&'是什么意思? Is 'Line (void)' the class 'Line'? 'Line(void)'是'Line'类吗? If so, what does '(void)' mean in this case? 如果是这样,在这种情况下'(void)'是什么意思?
Line help_line ();
This declares a function, not a Line
. 这声明了一个函数,而不是一个Line
。 Use Line help_line;
使用Line help_line;
instead. 代替。
See: Most vexing parse: why doesn't A a(()); 参见: 最令人烦恼的解析:为什么不是A(()); work? 工作?
You have declared help_line
as a function taking no parameters and returning a Line
. 您已将help_line
声明为不带参数且返回Line
的函数。 Is that what you intended? 这是你的意图吗?
If so, then you need to invoke the function, like this: 如果是这样,那么你需要调用这个函数,如下所示:
Line help_line();
...
figure_line.push_back(help_line());
If not, and you intended to declare help_line
as an object of type Line
, you need this: 如果没有,并且您打算将help_line
声明为Line
类型的对象,则需要:
Line help_line;
...
figure_line.push_back(help_line);
Line help_line ();
This does not mean " help_line
shall be an instance of Line
created with the default constructor". 这并不意味着“ help_line
应该是使用默认构造函数创建的Line
的实例”。 It means " help_line
shall be a function, implemented somewhere else, that takes no arguments and returns a Line
instance". 这意味着“ help_line
应该是一个函数,在其他地方实现,不带参数并返回Line
实例”。
The thing you want is spelled Line help_line;
你想要的东西拼写为Line help_line;
, with no parentheses. ,没有括号。
So, you get the following error message: 因此,您收到以下错误消息:
'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Line (void)' to 'Line &&'
Line &&
is the kind of parameter that push_back
is expecting. Line &&
是push_back
期望的那种参数。 The &&
doesn't really matter here; &&
在这里并不重要; it's best thought of, for beginners, as a kind of calling convention. 对初学者来说,最好的想法是作为一种召唤惯例。 You're still just passing a Line
, because that's the kind of thing you collect in a vector of Line
s. 你仍然只是传递一条Line
,因为那是你在Line
s矢量中收集的那种东西。
Line(void)
is "the type of functions that take no arguments and return a Line
instance". Line(void)
是“不带参数并返回Line
实例的函数类型”。 (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). (void)
是write ()
另一种方式,用于函数参数(在新代码中不鼓励,但有时在与非常旧的C代码交互时需要)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.