小编use*_*r16的帖子

使用聚合初始化和成员初始值设定项初始化结构

请考虑以下示例:

#include <iostream>
#include <string>
struct ABC
{
    std::string str;
    unsigned int id ;/* = 0 : error: no matching constructor for initialization of 'ABC'*/
};

int main()
{
    ABC abc{"hi", 0};
    std::cout << abc.str << " " << abc.id <<   std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

在定义结构ABC而没有id clang 3.x和gcc 4.8.x的默认值时,编译代码没有问题.但是,在为"id"添加默认参数后,我得到了流动的错误消息:

13 : error: no matching constructor for initialization of 'ABC'
ABC abc{"hi", 0};
^ ~~~~~~~~~
4 : note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 2 …
Run Code Online (Sandbox Code Playgroud)

c++ constructor aggregate-initialization c++11

16
推荐指数
1
解决办法
4260
查看次数