我想将一个数组传递给构造函数,但只有第一个值被传递了——其余的看起来像垃圾。
这是我正在处理的简化版本:
#include <iostream>
class board
{
public:
int state[64];
board(int arr[])
{
*state = *arr;
}
void print();
};
void board::print()
{
for (int y=0; y<8; y++)
{
for (int x=0; x<8; x++)
std::cout << state[x + y*8] << " ";
std::cout << "\n";
}
}
int main()
{
int test[64] = {
0, 1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7, 8,
2, 3, 4, 5, 6, 7, 8, 9,
3, 4, 5, 6, 7, 8, 9,10,
4, 5, 6, 7, 8, 9,10,11,
5, 6, 7, 8, 9,10,11,12,
6, 7, 8, 9,10,11,12,13,
7, 8, 9,10,11,12,13,14 };
board b(test);
b.print();
std::cin.get();
return 0;
}
能否有人解释一下为什么这个方法不起作用,以及如何正确地传递一个数组?此外,我不想复制这个数组。(还有,我真的必须对每行代码缩进4个空格吗?这太繁琐了。)
{}
- 的按钮。 - Michael Burr