c ++将函数放在类中

时间:2015-04-10 11:30:22

标签: c++ function class

您好我是c ​​++的新手并且已经开始研究Classes。

我在下面有一段简单的迷宫游戏代码。

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

而不是将主函数Id中的所有代码都放在一个类中。 这是我试过的。

新的mazefam.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;
}
}

我得到的错误,

失踪;在使用之前

找不到

'print'标识符

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你错过了';'在你的班级宣言结束时:

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

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

class maze
{
private:
  void print(char maze[10][9])
  [...]

public:
  int mazegame()
  [...]
}; // Still here !
相关问题