Hi all,

I'm doing a 3d pacman game for a university project which has to be written in c++ (3rd year and having never been taught any c++) My problem is that when I initialise the map array I cannot return the value back to the main method.

The values in the arrays are int's which will be used in constructing the maps (e.g 0 = wall 1 = nothing etc.)

When compiling, the line in the "getCell(x,y)" method gives the error which is "'map' undeclared (first use this function)" and if I put "this->map" it gives an "invalid use of 'this' in non-member function"

Any healp would be greatly appreciated.

My mapInit header:

//Header file for the map initialiser
#ifndef MAPINIT_H_
#define MAPINIT_H_

#include <windows.h>		// Header File For Windows
#include <math.h>			// Header File For Windows Math Library
#include <stdio.h>			// Header File For Standard Input/Output
#include <stdarg.h>			// Header File For Variable Argument Routines
#include <gl\gl.h>			// Header File For The OpenGL32 Library
#include <gl\glu.h>			// Header File For The GLu32 Library
#include <gl\glaux.h>		// Header File For The Glaux Library


//Class mapInit begin
class mapInit
{
      public:
      unsigned int getCell(unsigned int x, unsigned int y);
      void setMap( unsigned int mapSet );
      
      private:
      unsigned int map[22][19];
};

#endif

My mapInit class:

//Map initialization class

//Code begin

#include "mapInit.h"

void mapInit::setMap( unsigned int mapSet )
{    
     if ( mapSet == 1 )
     {
          unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                  {0,6,6,6,6,6,9,9,9,9,9,9,6,6,6,6,6,6,0},
                  {0,6,0,0,0,0,0,0,6,0,6,0,0,0,0,0,0,6,0},
                  {0,6,6,8,8,0,6,6,6,0,6,6,6,0,6,6,6,6,0},
                  {0,0,6,0,6,0,6,0,0,0,0,0,6,0,8,0,6,0,0},
                  {0,7,6,0,6,6,6,6,6,2,6,6,6,8,8,0,6,7,0},
                  {0,6,0,0,6,0,0,0,6,0,6,0,0,0,8,0,0,6,0},
                  {0,6,8,8,6,6,6,6,6,0,6,6,6,6,6,6,6,6,0},
                  {0,0,0,0,6,0,1,0,0,0,0,0,1,0,6,0,0,0,0},
                  {0,0,0,0,6,0,1,1,1,12,1,1,1,0,9,0,0,0,0},
                  {0,0,0,0,6,0,1,0,0,0,0,0,1,0,9,0,0,0,0},
                  {1,1,1,1,6,1,1,0,4,3,4,0,1,1,9,1,1,1,1},
                  {0,0,0,0,6,0,1,0,0,5,0,0,1,0,9,0,0,0,0},
                  {0,0,0,0,6,0,1,1,1,1,1,1,1,0,9,0,0,0,0},
                  {0,0,0,0,9,0,0,0,1,0,1,0,0,0,6,0,0,0,0},
                  {0,6,6,6,9,0,6,6,6,0,6,6,6,0,6,6,6,6,0},
                  {0,6,0,0,9,0,6,0,0,0,0,0,6,0,6,0,0,6,0},
                  {0,6,6,6,9,6,6,6,6,8,8,8,6,6,6,6,6,6,0},
                  {0,6,0,0,9,0,0,0,6,0,6,0,0,0,6,0,0,6,0},
                  {0,7,0,0,6,0,0,0,6,0,6,0,0,0,6,0,0,7,0},
                  {0,6,6,6,6,8,8,8,6,0,6,6,6,6,6,6,6,6,0},
                  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
                  
                  this->map[22][19] = mapBuffer[22][19];
     }
     else if ( mapSet == 2 )
     {
          unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
                  {0,6,6,6,6,6,6,6,0,1,0,6,6,6,6,8,8,6,0},
                  {0,6,0,0,6,0,0,6,0,1,0,6,0,0,6,0,0,6,0},
                  {0,7,0,0,6,6,6,6,0,1,0,6,8,8,6,0,0,7,0},
                  {0,6,0,0,6,0,0,6,6,2,6,6,0,0,6,0,0,8,0},
                  {0,6,6,6,6,6,6,0,6,0,6,0,6,6,6,6,6,6,0},
                  {0,9,0,0,6,0,6,0,6,0,6,0,6,0,6,0,0,8,0},
                  {0,9,0,0,6,0,6,0,6,0,6,0,6,0,6,0,0,6,0},
                  {0,9,6,8,8,8,6,1,1,1,1,1,9,9,9,9,6,8,0},
                  {0,9,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,9,0},
                  {0,9,6,6,0,0,0,4,4,3,4,4,0,0,0,6,6,9,0},
                  {0,9,0,6,8,6,0,0,0,0,0,0,0,6,6,6,0,9,0},
                  {0,6,6,0,0,6,0,10,10,10,10,10,0,9,0,0,6,6,0},
                  {0,0,6,0,0,6,0,10,0,1,0,10,0,9,0,0,6,0,0},
                  {0,6,6,0,0,6,10,10,0,1,0,10,10,9,0,0,6,6,0},
                  {0,6,0,0,6,6,0,0,0,12,0,0,0,6,6,0,0,6,0},
                  {0,6,6,0,6,0,8,6,6,0,6,6,6,0,6,0,6,6,0},
                  {0,0,6,8,6,6,8,0,6,6,6,0,8,6,6,6,6,0,0},
                  {0,6,6,0,6,0,6,6,0,1,0,6,6,0,6,0,6,6,0},
                  {0,6,0,6,6,6,0,6,0,1,0,6,0,6,6,6,0,6,0},
                  {0,7,6,6,0,6,6,6,0,1,0,6,6,6,0,6,6,7,0},
                  {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0} };
                  
                  this->map[22][19] = mapBuffer[22][19];
     }
     else if ( mapSet == 3 )
     {
          unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                  {0,7,6,6,0,6,6,6,0,6,6,6,0,6,6,6,6,7,0},
                  {1,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,1},
                  {0,6,0,6,0,6,6,6,0,9,0,8,6,6,0,6,0,6,0},
                  {0,6,0,9,0,6,0,6,8,9,0,6,0,8,6,6,0,6,0},
                  {0,6,6,9,6,6,0,6,0,9,6,6,0,6,0,6,6,6,0},
                  {0,6,0,9,0,6,0,6,0,9,0,6,0,8,0,11,0,0,0},
                  {0,6,0,6,0,8,6,6,0,9,0,9,0,6,6,11,5,4,0},
                  {0,6,0,8,6,6,0,6,0,6,2,9,6,6,0,11,0,4,0},
                  {0,6,6,6,0,9,0,6,6,6,0,9,0,6,0,11,0,4,0},
                  {0,6,0,8,0,9,0,6,0,6,0,9,0,6,0,11,5,3,0},
                  {0,6,0,8,0,9,6,6,0,6,0,9,0,6,0,11,0,4,0},
                  {0,6,0,6,0,9,0,6,0,9,0,9,0,6,6,11,0,4,0},
                  {0,8,6,6,0,6,0,6,6,9,6,9,0,6,0,11,5,4,0},
                  {0,8,0,6,6,6,0,6,0,9,0,9,6,6,0,11,0,0,0},
                  {0,8,0,6,0,6,0,6,0,6,0,9,0,6,0,6,6,6,0},
                  {0,8,6,6,0,6,6,6,0,6,0,6,0,6,6,8,0,6,0},
                  {0,6,0,6,0,6,0,10,0,6,6,6,0,6,0,8,0,6,0},
                  {0,6,0,6,6,6,0,10,0,6,0,6,6,6,0,8,0,6,0},
                  {1,6,0,6,0,6,0,10,0,6,0,6,0,6,0,8,0,6,1},
                  {0,7,6,6,0,6,0,12,0,6,6,6,0,6,6,6,6,7,0},
                  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
                  
                  this->map[22][19] = mapBuffer[22][19];
     }
     else if ( mapSet == 4 )
     {
          unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                  {0,7,6,6,6,6,0,4,3,4,0,6,6,6,6,6,6,7,0},
                  {0,6,0,6,0,6,0,0,5,0,0,6,0,6,0,6,0,6,0},
                  {0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0},
                  {0,6,0,6,0,8,0,6,0,6,0,6,0,6,0,6,0,6,0},
                  {0,6,6,6,6,6,6,9,9,9,6,6,6,6,6,6,6,6,0},
                  {0,6,0,8,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0},
                  {0,6,6,6,6,6,6,6,2,9,9,9,9,9,6,6,6,6,0},
                  {0,6,0,8,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0},
                  {0,6,6,6,6,6,9,9,9,9,9,9,6,6,6,6,6,6,0},
                  {0,9,0,6,0,8,0,6,0,6,0,8,0,6,0,6,0,6,0},
                  {0,9,6,8,6,6,9,9,9,9,9,9,6,6,6,6,6,6,0},
                  {0,9,0,6,0,6,0,0,0,6,0,6,0,8,0,6,0,6,0},
                  {0,9,6,6,6,6,0,10,10,6,6,6,6,9,6,9,6,9,0},
                  {0,9,0,6,0,6,0,10,0,0,0,0,0,9,0,9,0,9,0},
                  {0,9,6,8,6,6,0,10,10,10,10,12,0,9,6,9,6,9,0},
                  {0,6,0,6,0,6,0,0,0,0,0,0,0,6,0,6,0,9,0},
                  {0,6,6,6,6,6,6,6,8,6,8,8,8,8,6,6,6,6,0},
                  {0,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0},
                  {0,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0,6,0},
                  {0,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,0},
                  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
                  
                  this->map[22][19] = mapBuffer[22][19];
     }
     else if ( mapSet == 5 )
     {
          
          unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
                  {0,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,0},
                  {0,6,0,0,0,0,0,0,6,0,6,0,0,5,0,0,0,6,0},
                  {0,6,0,12,10,0,6,6,6,0,6,0,4,3,4,4,0,6,0},
                  {0,6,0,0,10,10,6,0,6,0,6,0,0,0,5,0,0,6,0},
                  {0,8,0,0,0,0,6,0,6,0,6,6,6,6,6,6,6,6,0},
                  {0,8,0,6,6,6,6,0,6,0,6,0,0,0,0,0,0,6,0},
                  {0,8,0,6,0,0,0,0,9,0,6,6,6,6,6,6,6,6,0},
                  {0,8,0,6,6,6,6,6,9,0,8,0,8,0,8,0,8,0,0},
                  {0,8,0,0,0,0,0,0,9,0,8,0,8,0,8,0,8,0,0},
                  {0,6,6,6,6,2,6,6,6,0,6,9,9,9,9,9,9,6,0},
                  {0,8,0,0,0,0,0,0,6,0,6,0,0,0,0,0,0,6,0},
                  {0,8,0,6,6,6,6,0,6,0,6,6,6,6,6,6,6,6,0},
                  {0,8,0,6,0,0,9,0,6,0,0,8,0,8,0,8,0,8,0},
                  {0,8,0,6,0,0,9,0,6,0,0,8,0,8,0,8,0,8,0},
                  {0,8,6,6,6,6,9,6,6,0,6,6,6,6,6,6,6,6,0},
                  {0,8,0,6,0,0,9,0,6,0,6,0,0,0,0,0,0,6,0},
                  {0,8,0,6,0,0,9,0,6,0,6,9,9,9,9,9,9,6,0},
                  {0,8,0,6,6,6,6,0,6,0,8,0,8,0,0,8,0,8,0},
                  {0,6,0,0,0,0,0,0,6,0,8,0,8,0,0,8,0,8,0},
                  {0,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,0},
                  {0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0} };
                  
                  this->map[22][19] = mapBuffer[22][19];
                  
                  
     }
}

unsigned int getCell(unsigned int x, unsigned int y)
{
         return this->map[x][y];
}

Arrays cannot be assigned.
You must copy each element in turn - two nested loops ought to do it.

Even if I ignore the array assignment and use mapBuffer[x][y] in the getCell(x,y) method it still gives the same error. The code I have there is after a lot of changes from how I originally did it and the only error it gives is the error in the line

return this->map[x][y];

Does getCell() know that it is part of your mapInit class? Hint: No, it doesn't.

> void mapInit::setMap( unsigned int mapSet ) What does this member function have....

> unsigned int getCell(unsigned int x, unsigned int y) That this 'member' function lacks?

Crap. So obvious lol but as I said, I've never been taught C++ before

Thanks :)

Oh, and while I'm on a "pointer-to-array" roll, consider this a very quick alternative to copying arrays (just point at them instead)

//Class mapInit begin
class mapInit
{
      public:
      unsigned int getCell(unsigned int x, unsigned int y);
      void setMap( unsigned int mapSet );
      
      private:
      unsigned int (*map)[19];
};

And

void mapInit::setMap( unsigned int mapSet )
{    
     if ( mapSet == 1 )
     {
          static unsigned int mapBuffer[22][19] = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                  {0,6,6,6,6,6,9,9,9,9,9,9,6,6,6,6,6,6,0},
                  {0,6,0,0,0,0,0,0,6,0,6,0,0,0,0,0,0,6,0},
                  {0,6,6,8,8,0,6,6,6,0,6,6,6,0,6,6,6,6,0},
                  {0,0,6,0,6,0,6,0,0,0,0,0,6,0,8,0,6,0,0},
                  {0,7,6,0,6,6,6,6,6,2,6,6,6,8,8,0,6,7,0},
                  {0,6,0,0,6,0,0,0,6,0,6,0,0,0,8,0,0,6,0},
                  {0,6,8,8,6,6,6,6,6,0,6,6,6,6,6,6,6,6,0},
                  {0,0,0,0,6,0,1,0,0,0,0,0,1,0,6,0,0,0,0},
                  {0,0,0,0,6,0,1,1,1,12,1,1,1,0,9,0,0,0,0},
                  {0,0,0,0,6,0,1,0,0,0,0,0,1,0,9,0,0,0,0},
                  {1,1,1,1,6,1,1,0,4,3,4,0,1,1,9,1,1,1,1},
                  {0,0,0,0,6,0,1,0,0,5,0,0,1,0,9,0,0,0,0},
                  {0,0,0,0,6,0,1,1,1,1,1,1,1,0,9,0,0,0,0},
                  {0,0,0,0,9,0,0,0,1,0,1,0,0,0,6,0,0,0,0},
                  {0,6,6,6,9,0,6,6,6,0,6,6,6,0,6,6,6,6,0},
                  {0,6,0,0,9,0,6,0,0,0,0,0,6,0,6,0,0,6,0},
                  {0,6,6,6,9,6,6,6,6,8,8,8,6,6,6,6,6,6,0},
                  {0,6,0,0,9,0,0,0,6,0,6,0,0,0,6,0,0,6,0},
                  {0,7,0,0,6,0,0,0,6,0,6,0,0,0,6,0,0,7,0},
                  {0,6,6,6,6,8,8,8,6,0,6,6,6,6,6,6,6,6,0},
                  {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} };
                  
                  this->map = mapBuffer;
     }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.