c++ - 如何在 C++ 中的结构中设置一个 int 数组

标签 c++ arrays struct

我有

struct board{
    int x[3];
    int y[3];
};

// in the class PatternReader
board PatternReader::testPattern1DX() {
   struct board aBoard;
   int x[3] = { 1,1,1 };
   aBoard = x;
   return aBoard;
}

错误是“int * 赋值中的不兼容类型”。

如何设置结构内的数组?

最佳答案

您不能分配数组。但是,您可以初始化结构:

board PatternReader::testPattern1DX() 
{
   board aBoard = { 
       {1, 1, 1}, 
       {2, 2, 2} 
   };
   return aBoard;
}

这会初始化 y 以及 x

关于c++ - 如何在 C++ 中的结构中设置一个 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21586598/

相关文章:

c++ - 如何在我的项目中包含常量

c++ - GCC 无法矢量化 64 位乘法。可以在 AVX2 上矢量化 64 位 x 64 位 -> 128 位加宽乘法吗?

javascript - 表 html 到数组

c - 如何从 inode 创建 struct vfsmount?

struct - 如何从字符串_and_创建迭代器并将其存储在结构中?

c++ - llvm::Module 是运算符 .* 和 ->* 的左侧

c++ - 为什么 FindWindow 找到了 EnumChildWindows 找不到的窗口?

c++ - 使用 initializer_list 初始化数组?

java - 通过jSTL在jsp中显示数组值时出错

json - 隐藏结构体字段并使其同步字段的访问和修改的最佳方法是什么?