简体   繁体   English

是什么导致了以下聚合初始化问题?

[英]What caused the following aggregate initialization problem?

I am trying to reproduce the code explained in a textbook about learning c++.我正在尝试重现有关学习 c++ 的教科书中解释的代码。 The simplest one is given as follows.最简单的一种如下。 But it does not compile as expected.但它没有按预期编译。

#include<vector>

using namespace std;

struct Storage
{
 vector<int> data;
};

Storage GetStorage(vector<int> params)
{
    return Storage{ .data=params};
}

Intellisense gives me this error back: Intellisense 给了我这个错误:

Storege{(<error-type>)<error-constant>}
expected an expression C/C++(29)

You must set your language version to C++20 for designated initializers to work.您必须将语言版本设置为 C++20 才能使指定的初始化程序起作用。 On visual studio you must enable the flag /std:c++latest and -std=c++20 on other compilers在 Visual Studio 上,您必须在其他编译器上启用标志/std:c++latest-std=c++20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM