gpt4 book ai didi

c++ - Array initializer must be an initializer list 错误未显示

转载 作者:太空狗 更新时间:2023-10-29 20:51:51 26 4
gpt4 key购买 nike

我很难弄清楚这段代码为何有效。我不应该收到“数组初始值设定项必须是初始值设定项列表”错误吗?

#include <iostream>

class B {
public:
B() { std::cout << "B Constructor " << std::endl ; }
B(const B&) { std::cout << "B Copy Constructor " << std::endl ; }
~B() { std::cout << "B Destructor " << std::endl ; }
} ;

class A {
public:
A() { std::cout << "A Constructor " << std::endl ; }
A(const A& other) : bs(other.bs)
{ std::cout << "A Copy Constructor " << std::endl ;}
~A() { std::cout << "A Destructor " << std::endl ; }
private:
B bs[12] ;
};


int main() {
A a ;
A b(a) ;
return 0 ;
}

g++ --version 的输出是 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)

最佳答案

格式错误,GCC 有一个错误。您指定 GCC 4.4.7,所以我将根据 C++11 来回答这个问题。初始化器的含义由 [dcl.init]/16 决定:

The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

  • the initializer is a (non-parenthesized) braced-init-list, the object or reference is list-initialized ([dcl.init.list]).
  • If the destination type is a reference type, see [dcl.init.ref].
  • If the destination type is an array of characters, an array of char16_t, an array of char32_t, or an array of wchar_t, and the initializer is a string literal, see [dcl.init.string].
  • If the initializer is (), the object is value-initialized.
  • Otherwise, if the destination type is an array, the program is ill-formed.
  • [... Continued but irrelevant here ... ]

必须检查项目符号以确定目标类型的初始值设定项的含义。由于您使用 (other.bs) 作为初始值设定项,并且目标类型是数组,因此唯一适用的项目符号是我突出显示的项目符号。程序格式错误。

关于c++ - Array initializer must be an initializer list 错误未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48307692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com