C++将对象添加到矢量时出错
我是矢量新手。我正在尝试将对象添加到向量中。但是程序无法编译,因为我的代码有问题。但我不知道那是什么。错误是:
error C2664: 'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Line (void)' to 'Line &&'
代码是:
Line help_line ();
cin >> ln_quan;
vector <Line> figure_line;
for (int i = 0 ; i < ln_quan ; i++)
{
figure_line.push_back(help_line);
}
编译器说错误在第 6 行(figure_line.push_back(help_line);)。
我放弃了试图找到一个解释如何添加对象的教程(在做这样的事情时我很容易放弃......)。
'Line (void)' 和 'Line &&' 是什么意思意思是? “Line (void)”是“Line”类吗?如果是这样,在这种情况下“(void)”是什么意思?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这声明了一个函数,而不是一个
Line
。请改用Line help_line;
。请参阅:最令人烦恼的解析:为什么不 A a(());工作?
This declares a function, not a
Line
. UseLine help_line;
instead.See: Most vexing parse: why doesn't A a(()); work?
您已将
help_line
声明为不带参数并返回Line
的函数。这就是你的意图吗?如果是这样,那么您需要调用该函数,如下所示:
如果不是,并且您打算将
help_line
声明为Line
类型的对象,则需要这样:You have declared
help_line
as a function taking no parameters and returning aLine
. Is that what you intended?If so, then you need to invoke the function, like this:
If not, and you intended to declare
help_line
as an object of typeLine
, you need this:这并不意味着“
help_line
应该是使用默认构造函数创建的Line
实例”。这意味着“help_line
应该是一个函数,在其他地方实现,不带参数并返回一个Line
实例”。您想要的内容拼写为
Line help_line;
,不带括号。因此,您会收到以下错误消息:
Line &&
是push_back
所期望的参数类型。&&
在这里并不重要;对于初学者来说,最好将其视为一种调用约定。您仍然只是传递一个Line
,因为这是您在Line
向量中收集的内容。Line(void)
是“不带参数并返回Line
实例的函数类型”。(void)
是函数参数的另一种编写()
的方法(在新代码中不鼓励这样做,但有时在与非常旧的 C 代码交互时需要)。This does not mean "
help_line
shall be an instance ofLine
created with the default constructor". It means "help_line
shall be a function, implemented somewhere else, that takes no arguments and returns aLine
instance".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_back
is 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 ofLine
s.Line(void)
is "the type of functions that take no arguments and return aLine
instance".(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).