1

Hi i am new to c++ and have begun looking into Classes.

i have this code below for a Simple maze game.

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

Instead of having all of this code in Main function Id like to put it as a class. Here is what i tried.

New mazefam.cpp file

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

And the header file i created, 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;
}
}

And the Errors i get,

missing ; before using

and

'print' identifier not found

A noob question but help fixing this would be great :D I dont even know if what im trying to do makes sense, apologies if it doesnt

edit: restructured, i think its more correct now

  • 1
    Try to re-read how to build up classes, afaik never put real functions (except for small ones) in headers, always keep them in cpp-files. Only put the function-headers in the header-file. – arc_lupus Apr 10 '15 at 11:33
  • 1
    `void print(char maze[10][9])();` why is there a `()` at the end ?? – Othman Benchekroun Apr 10 '15 at 11:34
  • I've played around with it all again, and i think its almost correct? –  Apr 10 '15 at 11:52
  • 1
    There has to be a `;` at the end of the class definition. `class mazz { .... };`, and no `;` behind the `#include`. In this case it fixed the code error because the preprocessor simply copies the header in place of the `#include`. But C++ class definitions must end with an `;`, because the `{ ... }` is not a block as in functions. – tmlen Apr 10 '15 at 11:54
  • @tmlen thanks for the help! one last error is, 'print' identifier not found, it is something to do with making print a global variable? –  Apr 10 '15 at 11:59
  • @hennessyd try to avoid global variables, especially in C++. Think object : to print your board is another function of your game, so you can put it in your game class. – Aracthor Apr 10 '15 at 12:25
  • You need to declare `print ` in `maze.h`. By adding `void print(char maze[10][9]);` in the header. Also instead of implementing `maze::mazegame()` in the header, it should just declare it, and the definition `void maze::mazegame() { ... code ... }` is better in the .cpp. – tmlen Apr 10 '15 at 12:27

2 Answers2

0

You must declare void print(char maze[10][9]) before it is used in class maze. Furthermore, it is calling for trouble to have a member with identical name to its class (class maze and char[][9] maze::maze). If maze::maze is never altered, you should make it a const member, so that the compiler complains should you accidently try to modify it. Finally, never say using namespace std; in a header file (and best also not in a source file), see this question for why.

Community
  • 1
  • 1
Walter
  • 44,150
  • 20
  • 113
  • 196
  • Thankyou, i've changed the names. But i am still having problems with declaring void print(char maze[10][9]), first time i have done this –  Apr 10 '15 at 12:15
0

You are missing a ';' at the end of your class declaration :

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

And to use your print function in your maze.h code, you need a prototype of your print function before it. But it would be simpler to make print a private member function of your maze class instead of independent function, that would make you able to call it from other functions of your class :

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

public:
  int mazegame()
  [...]
}; // Still here !
Aracthor
  • 5,757
  • 6
  • 31
  • 59