用大小初始化类成员向量失败 [英] Initializing class member vector with size fails

查看:139
本文介绍了用大小初始化类成员向量失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,遇到了这个问题。这是我的代码:

  A类
{
std :: vector< int> vec(10);
};

给我一​​个错误,说期望类型说明符。我知道这种初始化向量的方法是不可接受的,应该在构造函数中完成,但我很好奇为什么会失败。我还注意到:

  A类
{
std :: vector< int> vec = std :: vector< int>(10);
};

工作正常。



所以我的问题是,即使我们仍在创建具有预定义大小的向量( std :: vector< int>(10)),为什么第二种情况仍然有效,为什么第一种情况会起作用案子失败了吗?谢谢。



PS
我正在尝试创建尺寸为10个整数的矢量,而不是使用<$创建矢量c $ c> 10 已插入其中。

解决方案

什么?



自C ++ 11起,我们有了非静态数据成员初始化程序,使这种初始化成员的方法完全有效。 但是,您必须使用 = {}



形式上,如果此处存在初始化程序,则它必须是并带有 括号或等号初始化器 语法生成。



使用了此处不算作初始化的语法,编译器尝试使用 vec 进行函数声明,因为 20 不是类型。






为什么?



原始功能建议中名为问题1的章节( N2756 )解释说,此处不允许使用(),以避免在成员函数声明方面引起混淆。



作者在其中确实指出,委员会可以放任其存在,并使其像其他声明一样工作—。也就是说,它是一个函数(如果可以),否则是对象*。但是作者决定改用其他方法,只是禁止使用语法以避免这种情况下的问题。


不幸的是,这使得在解析声明时( expression-list )形式的初始化程序不明确 [..]



一种可能的解决方案是依赖现有规则,即如果声明可以是对象或函数,那么它就是函数 [。 ]



一个类似的解决方案是应用另一个现有规则,当前仅在模板中使用,如果 T 可以是类型,也可以是其他。如果确实是类型 [..]



<,我们可以使用 typename p> 这两种解决方案都引入了可能被许多用户误解的微妙(如comp.lang.c ++上有关 int i( ; 在块范围内未声明默认初始化的 int )。



本文提出的解决方案是仅允许使用 = initializer-clause 和 { initializer-list }形式的初始化器。可以解决大多数情况下的歧义问题。


这就是生命。



< hr>

所以?




我正在尝试创建大小为10个整数的矢量,不要创建已经插入10个向量。


这很重要。请注意,因为使用 {} 不会这样做。不幸的是,统一初始化仅添加了更多含义。因此,解决歧义问题非常重要



因此,您应该使用 = 语法,或者仅在






*这是最烦人的解析有时会咬你,尽管并非每次意外的函数声明都是最烦人的解析的一个例子。


I'm new with c++ and came across to this problem. Here is my code:

class A
{
    std::vector <int> vec(10);
};

It gives me error saying expected a type specifier. I know that this way of initializing a vector is not acceptable and it should be done in constructor but I'm curious why this fails. I also noticed that this:

class A
{
    std::vector <int> vec = std::vector<int>(10);
};

Works perfectly fine.

So my question is why does second case work even though we are still creating vector with "predefined size"(std::vector<int>(10)), and why does the first case fail? thanks.

P.S I'm trying to create vector with size for 10 ints, not create a vector with with 10 already inserted in it.

解决方案

What?

Since C++11 we have non-static data member initialisers, making this approach to initialising members completely valid. However, you must use either = or {}.

Formally, if an initializer is present here, it must be with a brace-or-equal-initializer grammatical production.

Since you've instead used a syntax that doesn't count as initialisation here, the compiler tries to make a function declaration out of vec, which breaks because 20 is not a type.


Why?

The chapter named "Problem 1" in the original feature proposal (N2756) explains that () was disallowed here to avoid some confusion with respect to member function declarations.

The author does note therein that the committee could have let it be, and had it work like other declarations — that is, it's a function if it can be, and an object otherwise*. But the author decided to go a different direction and just ban the syntax to avoid the problem in this particular case.

Unfortunately, this makes initializers of the "( expression-list )" form ambiguous at the time that the declaration is being parsed [..]

One possible solution is to rely on the existing rule that, if a declaration could be an object or a function, then it’s a function [..]

A similar solution would be to apply another existing rule, currently used only in templates, that if T could be a type or something else, then it’s something else; and we can use "typename" if we really mean a type [..]

Both of those solutions introduce subtleties that are likely to be misunderstood by many users (as evidenced by the many questions on comp.lang.c++ about why "int i();" at block scope doesn’t declare a default-initialized int).

The solution proposed in this paper is to allow only initializers of the "= initializer-clause" and "{ initializer-list }" forms. That solves the ambiguity problem in most cases.

Such is life.


So?

I'm trying to create vector with size for 10 ints, not create a vector with with 10 already inserted in it.

This is an important point. Be careful, because initialising a vector with {} won't do that. "Uniform" initialisation unfortunately merely added more meanings. So much for solving the ambiguity problem…

You should therefore use either the = syntax, or just do this in the constructor (my favourite).


* This is where the most vexing parse can sometimes bite you, though not every occurrence of unintended function declarations is an example of the most vexing parse.

这篇关于用大小初始化类成员向量失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆