C++将对象添加到矢量时出错

发布于 2024-12-22 12:31:26 字数 634 浏览 3 评论 0原文

我是矢量新手。我正在尝试将对象添加到向量中。但是程序无法编译,因为我的代码有问题。但我不知道那是什么。错误是:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

<逆流佳人身旁 2024-12-29 12:31:26
Line help_line ();

这声明了一个函数,而不是一个Line。请改用 Line help_line;

请参阅:最令人烦恼的解析:为什么不 A a(());工作?

Line help_line ();

This declares a function, not a Line. Use Line help_line; instead.

See: Most vexing parse: why doesn't A a(()); work?

荒人说梦 2024-12-29 12:31:26

您已将 help_line 声明为不带参数并返回 Line 的函数。这就是你的意图吗?

如果是这样,那么您需要调用该函数,如下所示:

Line help_line();
...
figure_line.push_back(help_line());

如果不是,并且您打算将 help_line 声明为 Line 类型的对象,则需要这样:

Line help_line;
...
figure_line.push_back(help_line);

You have declared help_line as a function taking no parameters and returning a 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:

Line help_line;
...
figure_line.push_back(help_line);
巡山小妖精 2024-12-29 12:31:26
Line help_line ();

并不意味着“help_line应该是使用默认构造函数创建的Line实例”。这意味着“help_line 应该是一个函数,在其他地方实现,不带参数并返回一个 Line 实例”。

您想要的内容拼写为 Line help_line;,不带括号。

因此,您会收到以下错误消息:

'void std::vector<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Line (void)' to 'Line &&'

Line &&push_back 所期望的参数类型。 && 在这里并不重要;对于初学者来说,最好将其视为一种调用约定。您仍然只是传递一个 Line,因为这是您在 Line 向量中收集的内容。

Line(void) 是“不带参数并返回 Line 实例的函数类型”。 (void) 是函数参数的另一种编写 () 的方法(在新代码中不鼓励这样做,但有时在与非常旧的 C 代码交互时需要)。

Line help_line ();

This does not mean "help_line shall be an instance of Line created with the default constructor". It means "help_line shall be a function, implemented somewhere else, that takes no arguments and returns a Line instance".

The thing you want is spelled 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. 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 Lines.

Line(void) is "the type of functions that take no arguments and return a Line 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文