gpt4 book ai didi

c++ - 结构和数组( ‘Array’ 的初始值设定项太多)C++

转载 作者:太空狗 更新时间:2023-10-29 23:50:43 29 4
gpt4 key购买 nike

为什么我得到错误:'Array' 的初始值设定项太多它的 C++11 代码,我不知道问题出在哪里

#include <iostream>
using namespace std;

struct Point {
int x,y ;
};
Point points[3] {{1,2},{3,4},{5,6}};
int x2 = points[2].x;

struct Array {
Point elem[3];
};

int main() {
cout << "!!!\nStructure!!!" << endl; //

Array points2 {{1,2},{3,4},{5,6}};// *too many initializers for‘Array’*
int y2 = points2.elem[2].y;

cout << "!!!here points2 = !!!" << y2 <<endl;

return 0;
}

最佳答案

您实际上还需要一组大括号来初始化该struct

Array points2  {{{1,2},{3,4},{5,6}}};
// ^Point
// ^Point[]
// ^Array

Working example

关于c++ - 结构和数组( ‘Array’ 的初始值设定项太多)C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28067885/

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