Sign in to follow this  
narnia649

Aid with Linking various documents

Recommended Posts

narnia649    138
I'm trying to get my program so that I don't have to keep creating new definitions for my maps. I want to be able to have it so that I have a map template that just draws the map based on a cpp file declaring what integer values the map should have (1, 2, 3, 4) and those integer values correspond to a char value of ('.','^',']'). I tried to do this, but I would get a Linker Error (I took out my map function and established it separate from main however void functions in main used variables in map). So basically, my question is how would I take this code here and make it so it's a template to create all my maps. Town.cpp
#include "Library.h"

bool n = false;
bool s = false;
bool e = false;
bool w = false;

const int Width = 10, Height = 10;

char pics[6] = {'@','.','^',']','a','*'};
int HeroX = 3, HeroY = 0;
const int Player = 0;
const int Grass = 1;
const int Mountain = 2;
const int Door = 3;
const int House = 4;
const int NewArea = 5;


int TownMap[Width][Height] = 
     {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{5, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 3, 1, 1, 3, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
};

void drawTown();
void loopTown();
void verifyDirection();
int main();
void Town()
{

drawTown();


}
void drawTown()
{

	system("cls");
	for (int y = 0; y < Height; y++)
	{
		for (int x = 0; x < Width; x++)
		{
           
			if (x == HeroX && y == HeroY)
			{   
				cout << pics[Player];
			}
			else
			{
				cout << pics[(TownMap[x][y])];
			}
		}
		
		cout << endl;
	}
   loopTown();

}

void loopTown()
{
	char direction;
	cout<<"> ";
	cin>> direction;

	switch(direction)
	{
	
	
      case 'w':
      
      n = true;
      verifyDirection();
      break;
      
      case 's':
      
      s = true;
      verifyDirection();
      break;  
      
      case 'd':
      
      w = true;
      verifyDirection();
      break;
      
      case 'a':
      
      e = true;
      verifyDirection();
      break;
      default:
      loopTown();              
     




	}



}
void verifyDirection()
{

if (n == true)
      {    
          if (TownMap[HeroX][HeroY - 1] != Mountain)
          {
           HeroX = HeroX;
           HeroY = HeroY - 1; 
           n = false;
           if (TownMap[HeroX][HeroY] == NewArea)
           {
            main();
           }
           drawTown();
           }
           else if(TownMap[HeroX][HeroY - 1] == Mountain)
           {
             n = false;
             drawTown();
           }
           }
       
     else if (e == true)
     {
         if (TownMap[HeroX - 1][HeroY] != Mountain)
         {
                      
         HeroX = HeroX - 1;
         HeroY = HeroY;
         e = false;
         if (TownMap[HeroX][HeroY] == NewArea)
           {
            main();
           }
         drawTown();
         }
         else if (TownMap[HeroX - 1][HeroY] == Mountain)
         {
         e = false;
         drawTown();
         }
     }
     else if (w == true)
     {
         if (TownMap[HeroX + 1][HeroY] != Mountain)
         {
         HeroX = HeroX + 1;
         HeroY = HeroY;
         w = false;
         if (TownMap[HeroX][HeroY] == NewArea)
           {
              main();
           }
         drawTown();
         }
         else if (TownMap[HeroX + 1][HeroY] == Mountain)
         {
          w = false;
          drawTown();     
         }
     }
     else if (s == true)
     {
         if (TownMap[HeroX][HeroY + 1] != Mountain)
         {
         HeroX = HeroX;
         HeroY = HeroY +1;
         s = false; 
         if (TownMap[HeroX][HeroY] == NewArea)
           {
            main();
           }  
         drawTown();  
          }
          else if (TownMap[HeroX][HeroY + 1] == Mountain)
          {
          s = false;
          drawTown();     
          }
     }
cin.ignore();

}

Main.cpp

#include "Library.h"
//find change of x and change of y to find positionX or positionY

bool north = false;
bool south = false;
bool west = false;
bool east = false;

char direction;


//starting point of character
int PlayerX = 2;
int PlayerY = 2;

char MapChars[6] = {'@','.','^',']','a','*'};
const int Player = 0;
const int Grass = 1;
const int Mountain = 2;
const int Door = 3;
const int House = 4;
const int NewArea = 5;


const int MapSizeX = 10, MapSizeY = 10;
int BaseMap[MapSizeX][MapSizeY] = 
     {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 5},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
};

void abovePlayer();
void reDraw();
void checkDirection();
void drawTown();
void Town();

int main()
{

 reDraw();
 return 0;   
}
void loopGame()
{    
     cout<< "> ";
     cin>> direction;
     switch(direction)
     {
      case 'w':
      
      north = true;
      checkDirection();
      break;
      
      case 's':
      
      south = true;
      checkDirection();
      break;  
      
      case 'd':
      
      west = true;
      checkDirection();
      break;
      
      case 'a':
      
      east = true;
      checkDirection();
      break;
      default:
      loopGame();              
     }
     
     
}
void reDraw()
{

    system("cls");
	for (int y = 0; y < MapSizeY; y++)
	{
		for (int x = 0; x < MapSizeX; x++)
		{
           
			if (x == PlayerX && y == PlayerY)
			{   
				cout << MapChars[Player];
			}
			else
			{
				cout << MapChars[(BaseMap[x][y])];
			}
		}
		
		cout << endl;
	}
   
   
	loopGame();
    
}

void checkDirection()
{
           
      if (north == true)
      {    
          if (BaseMap[PlayerX][PlayerY - 1] != Mountain)
          {
           PlayerX = PlayerX;
           PlayerY = PlayerY - 1; 
           north = false;
           if (BaseMap[PlayerX][PlayerY] == NewArea)
           {
            Town();
           }
           reDraw();
           }
           else if(BaseMap[PlayerX][PlayerY - 1] == Mountain)
           {
             north = false;
             reDraw();
           }
           }
       
     else if (east == true)
     {
         if (BaseMap[PlayerX - 1][PlayerY] != Mountain)
         {
                      
         PlayerX = PlayerX - 1;
         PlayerY = PlayerY;
         east = false;
         if (BaseMap[PlayerX][PlayerY] == NewArea)
           {
            Town();
           }
         reDraw();
         }
         else if (BaseMap[PlayerX - 1][PlayerY] == Mountain)
         {
         east = false;
         reDraw();
         }
     }
     else if (west == true)
     {
         if (BaseMap[PlayerX + 1][PlayerY] != Mountain)
         {
         PlayerX = PlayerX + 1;
         PlayerY = PlayerY;
         west = false;
         if (BaseMap[PlayerX][PlayerY] == NewArea)
           {
              Town();
           }
         reDraw();
         }
         else if (BaseMap[PlayerX + 1][PlayerY] == Mountain)
         {
          west = false;
          reDraw();     
         }
     }
     else if (south == true)
     {
         if (BaseMap[PlayerX][PlayerY + 1] != Mountain)
         {
         PlayerX = PlayerX;
         PlayerY = PlayerY +1;
         south = false; 
         if (BaseMap[PlayerX][PlayerY] == NewArea)
           {
            Town();
           }  
         reDraw();  
          }
          else if (BaseMap[PlayerX][PlayerY + 1] == Mountain)
          {
          south = false;
          reDraw();     
          }
     }
}


The only thing else is a Library.h file that has the #include <iostream> and the using namespace std;

Share this post


Link to post
Share on other sites
Stowelly    156
can you copy and paste the exact error please, will be easier to determine (im imagining it is due to some function declared with no body that you are calling, but would need the error to confirm this)

Share this post


Link to post
Share on other sites
narnia649    138
There is no error with the above code it is just tedious work to redeclare all the variables. The problem I'm having is renaming north, west, east, void functions, etc. when I need to make a new area to go to. I want to be able to define the dimensions for each area separately. What happens if I keep north/east/west the error I receive would be the following if I were to redeclare north instead of n inside of Town.cpp.
Linking...
Town.obj : error LNK2005: "bool north" (?north@@3_NA) already defined in Map.obj

Share this post


Link to post
Share on other sites
Stowelly    156
in your header file, before the includes put:

either


#pragma once



or


#ifndef RANDOM_HEADERFILE_INCLUDED
#define RANDOM_HEADERFILE_INCLUDED


//all your header file code here


#endif



the 2nd one is more cross platform safe afaik.

this is needed to ensure it only gets included in the project the one time

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this