C++ C++;返回多维数组
这是我目前正在进行的一个学校项目。我在返回多维数组C++ C++;返回多维数组,c++,C++,这是我目前正在进行的一个学校项目。我在返回多维数组Map[25][60]时遇到问题。方法get\u map()必须返回整个映射,以便我可以将其用作函数move\u left()中的参数 //ProjectX v3.0.cpp:定义控制台应用程序的入口点。 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 沃兹级 { int i; int j; 公众: wallz(); ~wallz(); 无效集i(int def_i); 无效集(int def_j); int get_i(); in
Map[25][60]
时遇到问题。方法get\u map()
必须返回整个映射,以便我可以将其用作函数move\u left()
中的参数
//ProjectX v3.0.cpp:定义控制台应用程序的入口点。
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
沃兹级
{
int i;
int j;
公众:
wallz();
~wallz();
无效集i(int def_i);
无效集(int def_j);
int get_i();
int get_j();
无效左移(整数映射[25][60],沃尔兹沃尔兹温度);
};
wallz::wallz()
{
}
沃兹::~wallz()
{
}
void wallz::set_i(int def_i)
{
i=定义i;
}
void wallz::set_j(int def_j)
{
j=def_j;
}
int wallz::get_i()
{
返回i;
}
int wallz::get_j()
{
返回j;
}
void wallz::向左移动(int Map[25][60],wallz wallz_temp)
{
int x=0;
而(x==0)
{
if(Map[wallz_temp.get_i()][wallz_temp.get_j()]==11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()]=0;
Map[wallz_temp.get_i()-1][wallz_temp.get_j()]=11;
}
if(Map[wallz_temp.get_i()-1][wallz_temp.get_j()]==11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()]=11;
Map[wallz_temp.get_i()-1][wallz_temp.get_j()]=0;
}
}
}
类图
{
wallz-wallz_质量[30];
int char_X;
int charuy;
int i;
int j;
国际地图[25][60];;
公众:
地图(国际地图[25][60]);;
~map();
void map_print();
void controlz();
int get_map();
wallz-get_-wallz(inth);
void map1_set_wallz();
void map2_set_wallz();
void map3_set_wallz();
void map4_set_wallz();
void map5_set_wallz();
无效左移();
无效向右移动();
void move_horizontal();
无效上移();
void move_down();
void move_vertical();
};
map::map(int m[25][60])
{
char_X=1;
char_Y=1;
对于(int i=0;ifrom
在C++中,不可能传递整个内存块。
由数组直接表示为参数的函数。但是
可以传递的是它的地址
几乎相同的效果,而且它是一种更快、更高效的方法
手术
您可能会发现论坛帖子有助于了解这一事实。下面是一个关于如何操作多维数组内容的快速示例(摘自论坛)
const int MAX_ROWS=4;
常数int MAX_COLS=4;
void printMatrix(int矩阵[MAX_ROWS][MAX_COLS],int r,int c)
{
对于(int i=0;使用astd:)array@CaptainObvlious在这个问题上修正了你的间距,但我建议你也在自己的副本中修正。
// ProjectX v3.0.cpp : Defines the entry point for the console application.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <thread>
#include <fstream>
using namespace std;
class wallz
{
int i;
int j;
public:
wallz();
~wallz();
void set_i(int def_i);
void set_j(int def_j);
int get_i();
int get_j();
void move_left(int Map[25][60], wallz wallz_temp);
};
wallz::wallz()
{
}
wallz::~wallz()
{
}
void wallz::set_i(int def_i)
{
i = def_i;
}
void wallz::set_j(int def_j)
{
j = def_j;
}
int wallz::get_i()
{
return i;
}
int wallz::get_j()
{
return j;
}
void wallz::move_left(int Map[25][60], wallz wallz_temp)
{
int x = 0;
while (x == 0)
{
if (Map[wallz_temp.get_i()][wallz_temp.get_j()] == 11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()] = 0;
Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] = 11;
}
if (Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] == 11)
{
Map[wallz_temp.get_i()][wallz_temp.get_j()] = 11;
Map[wallz_temp.get_i() - 1][wallz_temp.get_j()] = 0;
}
}
}
class map
{
wallz wallz_mass[30];
int char_X;
int char_Y;
int i;
int j;
int Map[25][60];
public:
map(int Map[25][60]);
~map();
void map_print();
void controlz();
int get_map();
wallz get_wallz(int h);
void map1_set_wallz();
void map2_set_wallz();
void map3_set_wallz();
void map4_set_wallz();
void map5_set_wallz();
void move_left();
void move_right();
void move_horizontal();
void move_up();
void move_down();
void move_vertical();
};
map::map(int m[25][60])
{
char_X = 1;
char_Y = 1;
for (int i = 0; i<25; i++)
for (int j = 0; j<60; j++)
Map[i][j] = m[i][j];
}
map::~map()
{
}
void map::map_print()
{
int MapCounter = 0;
for (int i = 0; i<25; i++)
{
for (int j = 0; j<60; j++)
{
if (MapCounter == 60)
{
cout << "" << endl;
MapCounter = 0;
}
if (i == char_X && j == char_Y)
{
cout << char(35);
}
else if (Map[i][j] == 1)
{
cout << char(186);//vertikalna liniq
}
else if (Map[i][j] == 0)
{
cout << " ";
}
else if (Map[i][j] == 2)//horizontalna liniq
{
cout << char(205);
}
else if (Map[i][j] == 3)
{
cout << char(200);//dolen lqv ugul
}
else if (Map[i][j] == 4)
{
cout << char(201);//goren lqv ugul
}
else if (Map[i][j] == 5)
{
cout << char(188);//dolen desen ugul
}
else if (Map[i][j] == 6)
{
cout << char(187);//goren desen ugul
}
else if (Map[i][j] == 7)
{
cout << char(204);//lqv vodoprovod
}
else if (Map[i][j] == 8)
{
cout << char(185);//desen vodoprovod
}
else if (Map[i][j] == 9)
{
cout << char(202);//goren vodoprovod
}
else if (Map[i][j] == 10)
{
cout << char(203);//desen vodoprovod
}
else if (Map[i][j] == 11)
{
cout << char(254); //
}
MapCounter++;
}
}
}
void map::controlz()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
if (Map[char_X - 1][char_Y] == 0)
{
char_X--;
}
}
if (GetAsyncKeyState(VK_DOWN) != 0)
{
if (Map[char_X + 1][char_Y] == 0)
{
char_X++;
}
}
if (GetAsyncKeyState(VK_LEFT) != 0)
{
if (Map[char_X][char_Y - 1] == 0)
{
char_Y--;
}
}
if (GetAsyncKeyState(VK_RIGHT) != 0)
{
if (Map[char_X][char_Y + 1] == 0)
{
char_Y++;
}
}
}
int map::get_map()
{
return Map[25][60];
}
wallz map::get_wallz(int h)
{
return wallz_mass[h];
}
void map::map1_set_wallz()
{
wallz_mass[0].set_i(6);
wallz_mass[0].set_j(2);
}
void move_left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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 3, 2, 2, 2, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 7, 2, 2, 11, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 2, 2, 2, 9, 2, 6, 2, 2, 2, 6, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 9, 2, 2, 2, 9, 2, 2, 2, 5,
};
map map1(Map);
bool stop = false;
while (stop == false)
{
map1.controlz();
map1.map_print();
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
}
thread first(move_left, map1);
first.join();
system("pause>nul");
return 0;
}
const int MAX_ROWS = 4;
const int MAX_COLS = 4;
void printMatrix(int matrix[MAX_ROWS][MAX_COLS], int r, int c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}