Archived

This topic is now archived and is closed to further replies.

Can't compile... Ugh...

This topic is 5525 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, I've been tryign to get this stuff to compile for about an hour now. I have tried in Dev-C++ and VC++ 6. Both give me errors about my objects in linking or something. Anyway, here is the code: main.cpp:
      
#ifndef BASICDATAH
     #define BASICDATAH
     #include "basicdata.h"
#endif

#ifndef GLOBALSH
     #define GLOBALSH
     #include "globals.h"
#endif

#ifndef MAPGENH
     #define MAPGENH
     #include "mapgen.h"
#endif

int main()
{
     plane[0] = genMap(79, 24, .03f, .10f, 20);
     dealloc2DArray(79, plane[0]);
     return 0;
}
  

mapgen.cpp:
      
/********** Headers **********/

//This file's header
#ifndef MAPGENH
     #define MAPGENH
     #include "mapgen.h"
#endif

/********** Functions **********/

//Generate a random maps of specified height, width, seeding, and clumping
tile** genMap(unsigned short xSize,                                                 //The x-size of the desired map
              unsigned short ySize,                                                 //The y-size of the desired map
              float seedC,                                                          //The seeding chance
              float clumpC,                                                         //The clumping chance
              unsigned short clumpL)                                                //The number of loops to go through when clumping
{
     unsigned short i, x, y;                                                        //Our counters
     tile **map = alloc2DArray<tile>(xSize, ySize);                                 //Allocate a map of the appropriate size
     srand(GetTickCount());                                                         //Seed the random number generator

     //Make it all water
     for (x = 0; x < xSize; x++)
     {
          for (y; y < ySize; y++)
          {
               map[x][y].ter = water;
               map[x][y].terRot = rand() % 3;
               map[x][y].bonus = noBonus;
               map[x][y].mod = noMod;
          }
     }

     //Now add some seed noise
     for (x = 0; x < xSize; x++)
     {
          for (y = 0; y < ySize; y++)
          {
               if (rand() / RAND_MAX < seedC)
               {
                    map[x][y].ter = grasslands;                                     //Creat grasslands
                    map[x][y].terRot = rand() % 3;                                  //Give the tile a random rotation
               }
          }
     }

     //Begin to clump the seed noise
     for (i; i < clumpL; i++)
     {
          for (x = 0; x < xSize; x++)
          {
               for (y = 0; y < ySize; y++)
               {
                    if ((map[x][y].ter = grasslands) && (rand() / RAND_MAX < clumpC))
                    {
                         if (x != 0)
                              map[x - 1][y].ter = grasslands;                       //Creat grasslands

                         if (x != xSize - 1)
                              map[x + 1][y].ter = grasslands;                       //Creat grasslands

                         if (y != 0)
                              map[x][y - 1].ter = grasslands;                       //Creat grasslands

                         if (y != ySize - 1)
                              map[x][y + 1].ter = grasslands;                       //Creat grasslands
                    }
               }
          }
     }

     //Now add tundra in the bottom and top 1.5% of the map (it must be at least one row)
     i = (unsigned short) (ySize * .015f);
     i = (i == 0 ? 0 : 1);

     for (x = 0; x < xSize; x++)
     {
          for (y = 0; y < i; y++)
          {
               map[x][y].ter = tundra;
               map[x][ySize - y - 1].ter = tundra;
          }
     }

     //Now add tundra on the land in the next 1.5% of the map (it must be at least one row)
     i = (unsigned short) (ySize * .03f);
     i = (i == 0 ? 0 : 1);

     for (x = 0; x < xSize; x++)
     {
          for (y = 0; y < i; y++)
          {
               if (map[x][y].ter != water)
                    map[x][y].ter = tundra;
          }
     }

     //Output the map to the console
     for (x = 0; x < xSize; x++)
     {
          for (y = 0; y < ySize; y++)
          {
               switch (map[x][y].ter)
               {
               case grasslands:
				 std::cout << "G";
               case tundra:
				 std::cout << "T";
               }
          }
		std::cout << std::endl;
     }

	return map;
}
  

mapgen.h:
      
/********** Headers **********/

//Our global variables
#ifndef GLOBALSH
     #define GLOBALSH
     #include "globals.h"
#endif

//Windows functions, used here for GetTickCount
#ifndef WINDOWSH
    #define WINDOWSH
    #include <windows.h>
#endif

//Standard library for random numbers
#ifndef STDLIBH
    #define STDLIBH
    #include <stdlib.h>
#endif

//My basic data fucntions
#ifndef BASICDATAH
     #define BASICDATAH
     #include "basicdata.h"
#endif

#include <iostream>

/********** Function Prototypes **********/

//Generate a random maps of specified height, width, seeding, and clumping

tile** genMap(unsigned short xSize,                                                 //The x-size of the desired map

              unsigned short ySize,                                                 //The y-size of the desired map

              float seedC,                                                          //The seeding chance

              float clumpC,                                                         //The clumping chance

              unsigned short clumpL);                                               //The number of loops to go through when clumping

  

globals.h:
      
//Define NULL

#ifndef NULL
     #define NULL 0
#endif

//Terrain types

enum terTypes
{
     water,
     shore,
     grasslands,
     desert,
     tundra,
     swamp,
     forest,
     river,
     riverM,
     lake,
     hills,
     mountain,
     volcano
};

//Bonus items

enum bonusTypes
{
     noBonus,
     coal,
     iron,
     silver,
     mithril,
     gold,
     gems,
     adamantium,
     cidryx,
     game,
     nShade,
     sorcNode,
     choasNode,
     natNode,
     temple,
     tower,
     keep,
     ruins,
     cave
};

//Modifier types

enum modTypes
{
     noMod,                                                                         //No road

     road,                                                                          //A road

     eRoad                                                                          //An enchanted road

};

//Shore rotations

enum shoreRots
{
     shoreN,                                                                        //Shore on the north

     shoreW,                                                                        //Shore on the west

     shoreE,                                                                        //Shore on the east

     shoreS,                                                                        //Shore on the south

     shoreNE,                                                                       //Shore in the north east corner

     shoreNW,                                                                       //Shore in the north west corner

     shoreSE,                                                                       //Shore in the south east corner

     shoreSW,                                                                       //Shore in the south west corner

     shoreNoN,                                                                      //No shore on the north

     shoreNoW,                                                                      //No shore on the west

     shoreNoE,                                                                      //No shore on the east

     shoreNoS,                                                                      //No shore on the south

     shoreNoNE,                                                                     //No shore in the north east corner

     shoreNoNW,                                                                     //No shore in the north west corner

     shoreNoSE,                                                                     //No shore in the south east corner

     shoreNoSW                                                                      //No shore in the south west corner

};

//The tile struct

struct tile
{
     unsigned char ter;                                                             //Terrain type

     unsigned char terRot;                                                          //The tile graphic

     unsigned char bonus;                                                           //Any bonuses

     unsigned char mod;                                                             //Move cost modifying objects

};

//The bonus struct

struct bonusTile
{
     unsigned char power;                                                           //The extra power given by the bonus

     unsigned char gold;                                                            //Extra gold

     unsigned char food;                                                            //Extra food

     float ucBonus;                                                                 //Percentage of price of units to take off

};

struct terTile
{
     float gold;                                                                    //Percentage of extra gold

     float prod;                                                                    //Percentage of extra production

     float food;                                                                    //Extra food

};

//Base values

float terCost[13] = {0, 0, 1, 1.5, 1.5, 2.5, 1.5, 1, 1, 0, 2, 3, 3};               //The base move csot of a peice of terrain

float terPop[13] = {0, .001, .004, .002, .002, .0015, .005, .006, .006, .005, .004, .0035, 0};     //Population growth

float terMaxPop[13] = {0, .2, .8, .4, .4, .3, 1, 1.2, 1.2, 1, .8, .7, 0};          //Maximum population in thousands on a tile


//Modifying values

float roadCostMods[3] = {1, .5, 0};

//Basic tile values

terTile terVals[13] = {{0, 0, 0},                                                   //Water

                         {0, .02, .5},                                              //Shore

                         {0, 0, 1.5},                                               //Grasslands

                         {0, .02, 0},                                               //Desert

                         {0, 0, 0},                                                 //Tundra

                         {0, 0, .5},                                                //Swamp

                         {0, .03, .5},                                              //Forest

                         {.03, .05, 2},                                             //River

                         {0, .04, 2},                                               //River mouth

                         {0, .02, 2},                                               //Lake

                         {0, .03, .5},                                              //Hills

                         {0, .05, 0},                                               //Mountain

                         {0, 0, 0}};                                                //Volcano


//Bonus values

bonusTile bonuses[19] = {{0, 0, 0, 0},                                              //Nothing

                          {0, 0, 0, .1},                                            //Coal

                          {0, 0, 0, .05},                                           //Iron

                          {0, 2, 0, 0},                                             //Silver

                          {1, 2, 0, 0},                                             //Mithril

                          {0, 3, 0, 0},                                             //Gold

                          {0, 5, 0, 0},                                             //Gems

                          {2, 0, 0, 0},                                             //Adamantium

                          {4, 0, 0, 0},                                             //Cydrix

                          {0, 0, 2, 0},                                             //Game

                          {0, 0, 0, 0},                                             //Night shad

                          {0, 0, 0, 0},                                             //Sorcery node

                          {0, 0, 0, 0},                                             //Chaos node

                          {0, 0, 0, 0},                                             //Nature node

                          {0, 0, 0, 0},                                             //Temple

                          {0, 0, 0, 0},                                             //Tower

                          {0, 0, 0, 0},                                             //Keep

                          {0, 0, 0, 0},                                             //Ruins

                          {0, 0, 0, 0}};                                            //Cave or lair


//Set up our pointers to the maps and make sure they are global

tile **plane[2] = {NULL, NULL};                                                     //The planes...

char curPlane = 0;                                                                  //The current plane


  
basicdata.h:
  
/********** Defines, Enum's and Consts **********/

/********** Struct's and union's **********/

//A 2D point
template <class T> struct point2D
{
     T x;
	T y;
};

//A 3D point
template <class T> struct point3D
{
     T x;
	T y;
	T z;
};

//An RGB value
template <class T> struct RGB
{
     T r;
	T g;
	T b;
};

//An RGBA value
template <class T> struct RGBA
{
     T r;
     T g;
     T b;
     T a;
};

union variant
{
     char c;
     unsigned char uc;
     short s;
     unsigned short us;
     long l;
     unsigned long ul;
     float f;
     void *ptr;
};

/********** Function Prototypes **********/

template <class T> inline T** alloc2DArray(unsigned short x, unsigned short y);     //Allocate a 2D array
template <class T> inline void dealloc2DArray(unsigned short x, T ***ary);          //De-allocate a 2D array
template <class T> void swap(T a, T b);                                             //Swap's two arbitrary variables

/********** Functions **********/

//Allocate a 2D array

template <class T> inline T** alloc2DArray(unsigned short x, unsigned short y)
{
     unsigned short i;
     T **ary;

     ary = new T* [x];
     for (i = 0; i < x; i++)
          ary[i] = new T[y];
     
     return ary;
}

//De-allocate a 2D array

template <class T> inline void dealloc2DArray(unsigned short x, T **ary)
{
     unsigned short i;

     for (i = 0; i < x; i++)
          delete [] ary[i];

     delete [] ary;
}

//Swap two arbitrary peices of data

template <class T> void swap(T a, T b)
{
     T c = a;
     a = b;
     b = c;
}
    
I put all of those in a project file for dev-c++ and when I try to compile the project from main.cpp, it give me " C:\Dev-Cpp\Makefile.win [Build Error] [../Documents and Settings/Ian/Desktop/Junk/mom clone/mapgen.o] Error 1"... From VC++ I get: --------------------Configuration: map - Win32 Debug-------------------- Compiling... mapgen.cpp c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(104) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(105) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(112) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(114) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(117) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(118) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(118) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(119) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(120) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(121) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(122) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(127) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\globals.h(128) : warning C4305: 'initializing' : truncation from 'const double' to 'float' c:\documents and settings\ian\desktop\junk\mom clone\basicdata.h(5) : warning C4005: 'TRUE' : macro redefinition c:\program files\microsoft visual studio\vc98\include\windef.h(67) : see previous definition of 'TRUE' Linking... mapgen.obj : error LNK2005: "char curPlane" (?curPlane@@3DA) already defined in main.obj mapgen.obj : error LNK2005: "struct tile * * * plane" (?plane@@3PAPAPAUtile@@A) already defined in main.obj mapgen.obj : error LNK2005: "struct bonusTile * bonuses" (?bonuses@@3PAUbonusTile@@A) already defined in main.obj mapgen.obj : error LNK2005: "struct terTile * terVals" (?terVals@@3PAUterTile@@A) already defined in main.obj mapgen.obj : error LNK2005: "float * roadCostMods" (?roadCostMods@@3PAMA) already defined in main.obj mapgen.obj : error LNK2005: "float * terMaxPop" (?terMaxPop@@3PAMA) already defined in main.obj mapgen.obj : error LNK2005: "float * terPop" (?terPop@@3PAMA) already defined in main.obj mapgen.obj : error LNK2005: "float * terCost" (?terCost@@3PAMA) already defined in main.obj Debug/map.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. map.exe - 9 error(s), 32 warning(s) [/source] If anyone has any ideas, please let me know... I have been workign on this for a while and would really like to see the results of it. [edited by - The Heretic on December 2, 2002 7:25:01 AM]

Share this post


Link to post
Share on other sites
This happens because your global variables are not declared
''static'' (if you define them in headers) or ''extern'' respectivly.

Just declare your variables in headers and define them in C file.

Example:

globals.h:

  
//Base values

extern float terCost[];
//The base move csot of a peice of terrain

extern float terPop[];
//Population growth

float terMaxPop[];
{/source]

globals.cpp:

[source]
//Base values

float terCost[13] = { ....... }; // <- insert actual values

//The base move csot of a peice of terrain

float terPop[13] = {....... }; // <- insert actual values

//Population growth

float terMaxPop[13] = { ....... }; // <- insert actual values

{/source]

Cheers,
Pat

Share this post


Link to post
Share on other sites