C ++返回多维数组

时间:2015-04-10 20:34:42

标签: c++

这是我正在研究的学校项目。我在返回多维数组Map[25][60]时遇到问题。方法get_map()必须返回整个地图,以便我可以将其用作函数move_left()中的参数。

// 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(map map1)
{
    map1.get_wallz(0).move_left(map1.get_map(), map1.get_wallz(0));
}

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 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;
}

1 个答案:

答案 0 :(得分:0)

来自here

  

在C ++中,无法传递整个内存块   由数组表示直接作为参数的函数。但   可以传递的是它的地址。在实践中,这有   几乎相同的效果,它更快,更有效   操作

您可能会发现this论坛帖子有助于了解这一事实。这是一个关于如何操作多维数组内容的快速示例(取自论坛)。

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;
    }   
}

另请参阅:How do I use arrays in c++?