Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板和初始化类成员_C++_Templates_Initialization - Fatal编程技术网

C++ 模板和初始化类成员

C++ 模板和初始化类成员,c++,templates,initialization,C++,Templates,Initialization,我很难理解为什么下面CC的定义会在错误消息之后生成错误(Visual Studio 2019)。有人能解释一下吗 错误(活动)E0415不存在从“int”转换为“C”的合适构造函数 谢谢 struct A { int i ; }; template< typename T > struct B { T i ; }; struct C : public B<int> { }; const A AA = { 1 }; // OK cons

我很难理解为什么下面CC的定义会在错误消息之后生成错误(Visual Studio 2019)。有人能解释一下吗

错误(活动)E0415不存在从“int”转换为“C”的合适构造函数

谢谢

struct A
{
    int i ;
};

template< typename T >
struct B
{
    T i ;
};

struct C 
    : public B<int>
{   
};

const A AA = { 1 }; // OK
const C CC = { 1 }; // ERROR
结构A { int i; }; 模板 结构B { TⅠ; }; 结构C :公共B { }; 常数A AA={1};//好啊 常数C CC={1};//错误
使用v19.24和
/std:c++17
。我不知道c++17,我有点老了。但是错误消息告诉你需要添加一个构造函数(或者,显然,移动到一个后来的C++版本)。要初始化的变量在基类函数中,因此需要一种方法将其传递给基类初始化器。@Basya:
C
是从C++17开始的聚合;大括号省略应适用。您正在使用
C
不是C++14及更早版本中的聚合,而是C++17及更高版本中的聚合。So
constccc={1}
在C++17中有效,但在C++14中无效。谢谢。将VS2019从“默认”更改为C++17,错误已消失。谢谢使用v19.24和
/std:c++17
。我不懂c++17,我有点老了。但是错误消息告诉你需要添加一个构造函数(或者,显然,移动到一个后来的C++版本)。要初始化的变量在基类函数中,因此需要一种方法将其传递给基类初始化器。@Basya:
C
是从C++17开始的聚合;大括号省略应适用。您正在使用
C
不是C++14及更早版本中的聚合,而是C++17及更高版本中的聚合。So
constccc={1}
在C++17中有效,但在C++14中无效。谢谢。将VS2019从“默认”更改为C++17,错误已消失。谢谢