c++ 将函数放在类中

c++ Putting a function in a class

本文关键字:函数 c++      更新时间:2023-10-16

嗨,我是 c++ 的新手,已经开始研究类。

我在下面有这个代码用于一个简单的迷宫游戏。

    #include <iostream>
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
    for(int j = 0; j < 9; j++)
    {
        cout << maze[i][j];
    }
    cout << endl;
}
}
int main()
{
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
                   { '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
                   { '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
                   { '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
                   { '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                   { '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
                   { '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
                   { '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
                   { '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                   { '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
    print(maze);
    char move = ' ';
    cout << "Move: ";
    while(move != 'w' && move != 's' && move != 'a' && move != 'd' &&    move != 'q')
    {
        cin >> move;
    }
    if (move == 'w')
    {
        if (maze[row - 1][col] == ' ')
        {
            maze[row][col] = ' ';
            row--;
            maze[row][col] = '*';
        }
        else if (maze[row - 1][col] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 's')
    {
        if (maze[row + 1][col] == ' ')
        {
            maze[row][col] = ' ';
            row++;
            maze[row][col] = '*';
        }
        else if (maze[row + 1][col] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'a')
    {
        if (maze[row][col - 1] == ' ')
        {
            maze[row][col] = ' ';
            col--;
            maze[row][col] = '*';
        }
        else if (maze[row][col - 1] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'd')
    {
        if (maze[row][col + 1] == ' ')
        {
            maze[row][col] = ' ';
            col++;
            maze[row][col] = '*';
        }
        else if (maze[row][col + 1] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'q')
    {
        gameOver = true;
    }
    else
    {
        cout << "Invalid Input" << endl;
    }
    }
    return 0;
    }

与其将所有这些代码都放在 Main 函数 ID 中,不如将其作为一个类。这是我尝试过的。

新迷宫.cpp文件

#include <iostream>
#include "maze.h"
using namespace std;
void print(char maze[10][9])
{
for(int i = 0; i < 10; i++)
{
    for(int j = 0; j < 9; j++)
    {
        cout << maze[i][j];
    }
    cout << endl;
}
}
int main()
{
maze mazenow;
mazenow.mazegame();
cout << "Hi world" << endl;
}

还有我创建的头文件,maze.h

 #include <iostream>
using namespace std;

class maze
{
public:
int mazegame(){
char maze[][9] = { { '+', '-', '-', '-', '-', '-', '-', '-', '+' },
                   { '|', ' ', ' ', ' ', '|', ' ', ' ', ' ', '|' },
                   { '|', ' ', '|', ' ', ' ', ' ', '|', ' ', '|' },
                   { '|', ' ', '+', '-', '-', '-', '+', ' ', '|' },
                   { '|', '#', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                   { '|', '-', '+', ' ', '+', ' ', '+', '-', '|' },
                   { '|', ' ', ' ', ' ', '|', ' ', '|', ' ', '|' },
                   { '|', ' ', '|', ' ', '+', '-', '+', ' ', '|' },
                   { '|', '*', '|', ' ', ' ', ' ', ' ', ' ', '|' },
                   { '+', '-', '-', '-', '-', '-', '-', '-', '+' } };
int row = 8;
int col = 1;
bool gameOver = false;
while(!gameOver)
{
    print(maze);
    char move = ' ';
    cout << "Move: ";
    while(move != 'w' && move != 's' && move != 'a' && move != 'd' && move != 'q')
    {
        cin >> move;
    }
    if (move == 'w')
    {
        if (maze[row - 1][col] == ' ')
        {
            maze[row][col] = ' ';
            row--;
            maze[row][col] = '*';
        }
        else if (maze[row - 1][col] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 's')
    {
        if (maze[row + 1][col] == ' ')
        {
            maze[row][col] = ' ';
            row++;
            maze[row][col] = '*';
        }
        else if (maze[row + 1][col] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'a')
    {
        if (maze[row][col - 1] == ' ')
        {
            maze[row][col] = ' ';
            col--;
            maze[row][col] = '*';
        }
        else if (maze[row][col - 1] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'd')
    {
        if (maze[row][col + 1] == ' ')
        {
            maze[row][col] = ' ';
            col++;
            maze[row][col] = '*';
        }
        else if (maze[row][col + 1] == '#')
        {
            gameOver = true;
        }
    }
    else if (move == 'q')
    {
        gameOver = true;
    }
    else
    {
        cout << "Invalid Input" << endl;
    }
   }
    return 0;
}
}

我得到的错误,

缺少 ; 使用前

未找到"打印"标识符

一个菜鸟问题,但帮助解决这个问题将是很好的:D我什至不知道我试图做的事情是否有意义,如果没有,请道歉

编辑:重组,我认为现在更正确

类声明末尾缺少一个";":

class maze
{
public:
   [...]
}; // Here !

要在maze.h代码中使用打印函数,您需要先有一个print函数的原型。但是print迷宫类的私有成员函数而不是独立函数会更简单,这将使您能够从类的其他函数调用它:

class maze
{
private:
  void print(char maze[10][9])
  [...]
public:
  int mazegame()
  [...]
}; // Still here !

class maze 中使用之前,必须声明void print(char maze[10][9])。此外,它要求有一个与其类(class mazechar[][9] maze::maze(同名的成员是麻烦的。如果从未更改maze::maze,则应将其设置为const成员,以便在您意外尝试修改它时编译器会抱怨。最后,永远不要在头文件中说using namespace std;(最好也不要在源文件中(,请参阅此问题以了解原因。