Używanie danych i funkcji z innych plików źródłowych

0

witam,
mam pewien problem, a mianowicie nie wiem jak sobie poradzić z tym żeby wykorzystać funkcje i dane z jednego .cpp w innym ( main.cpp )
Mam coś takiego w pathfind.cpp

 // Global data
// The world map
const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 10;

int map[ MAP_WIDTH * MAP_HEIGHT ] =
{
//0001020304050607080910111213141516171819
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,   // 00
  1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1,   // 01
  1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,   // 02
  1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,   // 03
  1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1,1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1,   // 04
  1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1,1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1,   // 05
  1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,   // 06
  1,9,9,9,9,9,9,9,3,2,1,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,9,1,1,1,9,9,9,9,9,9,9,1,   // 07
  1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,   // 08
  1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1,1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1,   // 09
  1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1,1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1,   // 10
};

// map helper functions---------------------------------------
int GetMap( int x, int y )
{
  if( x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT )
  {
    return 9;
  }
  return map[(y*MAP_WIDTH)+x];
}

i chcę użyć np. GetMap() w main.cpp w ktorym załączam nagłówek z pathfind ale nic w nim nie umieściłem związanego z powyższym kodem. Jeżeli chciałbym np. umieścić deklarację GetMap() w pathfind.h to potrzebował bym znowu moje stałe MAP_WIDTH i MAP_HEIGHT.
Wiem, że to jest banalne, ale nie mam teraz żadnego pamysłu na to.

1

pathfind.h

#ifndef __PATHFIND_H__
#define __PATHFIND_H__

 // Global data
// The world map
const int MAP_WIDTH = 20;
const int MAP_HEIGHT = 10;
 
extern int map[ MAP_WIDTH * MAP_HEIGHT ];
 
// map helper functions---------------------------------------
int GetMap( int x, int y );

#endif

pathfind.cpp

#include "pathfind.h"

int map[ MAP_WIDTH * MAP_HEIGHT ] =
{
//0001020304050607080910111213141516171819
  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,   // 00
  1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,1,   // 01
  1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,   // 02
  1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,1,9,9,1,1,9,9,9,1,9,1,9,1,9,1,9,9,9,1,1,   // 03
  1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1,1,9,1,1,1,1,9,9,1,9,1,9,1,1,1,1,9,9,1,1,   // 04
  1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1,1,9,1,1,9,1,1,1,1,9,1,1,1,1,9,1,1,1,1,1,   // 05
  1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,1,9,9,9,9,1,1,1,1,1,   // 06
  1,9,9,9,9,9,9,9,3,2,1,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,9,1,1,1,9,9,9,9,9,9,9,1,   // 07
  1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,1,9,1,1,1,1,1,1,1,1,   // 08
  1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1,1,9,1,9,9,9,9,9,9,9,1,1,9,9,9,9,9,9,9,1,   // 09
  1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1,1,9,1,1,1,1,9,1,1,9,1,1,1,1,1,1,1,1,1,1,   // 10
}; /* to się nie kompiluje. za dużo dałeś tych cyferek w stosunku do MAP_WIDTH*MAP_HEIGHT */

int GetMap( int x, int y )
{
  if( x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT )
  {
    return 9;
  }
  return map[(y*MAP_WIDTH)+x];
}

main.cpp

#include "pathfind.h"

int main()
{
  GetMap(whatever);
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1