gpt4 book ai didi

C++错误初始化数组

转载 作者:行者123 更新时间:2023-11-30 04:04:59 25 4
gpt4 key购买 nike

我想初始化数组car.places[2][3]但数组中始终为零。请有人告诉我我做错了什么,这是代码:

#include <iostream>
#include <string>

using namespace std;

class reserv
{
public:
int places[2][3];
} car;


int main () {

car.places[2][3] = (
(1, 2, 3),
(4, 5, 6)
);

for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
cout << i << "," << j << " " << car.places[i][j] << endl;
}
}

return 0;
}

我从编译器收到警告:

>g++ -Wall -pedantic F_car_test.cpp
F_car_test.cpp: In function 'int main()':
F_car_test.cpp:16:11: warning: left operand of comma operator has no effect [
-Wunused-value]
(1, 2, 3),
^
F_car_test.cpp:16:14: warning: right operand of comma operator has no effect
[-Wunused-value]
(1, 2, 3),
^
F_car_test.cpp:17:11: warning: left operand of comma operator has no effect [
-Wunused-value]
(4, 5, 6)
^
F_car_test.cpp:17:14: warning: right operand of comma operator has no effect
[-Wunused-value]
(4, 5, 6)
^

提前致谢

最佳答案

你不能在没有循环的声明之后做这件事。

这是在循环中执行的方法:

for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
car.places[i][j] = 1 + 3 * i + j;
}
}

关于C++错误初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23525487/

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