Sign in to follow this  
icecubeflower

multiple definition error with namespace in .h file

Recommended Posts

Hey I have a library that I made with about 12 .h and .cpp files. This is switchmaster.h:
#ifndef SWITCHMASTER
#define SWITCHMASTER

//day is x cycles long  (x milliseconds on fast machine)
#define DAYLEN 1800000
//x cycles to shift daylight
#define DAYSHIFT 2000.0f
#define CLOUDSHIFT 2000.0f

#define HORZ 1024
#define VERT 768
#define BPP 32

//max length of a filename this program will work with (exluding extension, I think so really IFILE+4)
#define IFILE 10
//half the size of the hero you want to draw, make this obsolete eventually
#define IHEROHALF 58
#define DVAL 800

//#define RATSLAM 3
//#define RATDAMAGE 0.5f

//the number of defined gizmo types -1
//0 = standard player gizmo | NO AI     | 32 PNG's, 1 GIZ
//1 = rat gizmo             | SIMPLE AI | 16 PNG's
//2 = simple mill           | SPIN      | 3 PNG's
//3 = stain                 |           | 4 PNG's
#define IGIZ 3
//number of defined anibox types -1
#define IANI 0
//the number of different possible layers -1
#define ILAYER 4

#define SCROLLSPEED 16.0f


//should only be three sections this effects
#define PROGRAMMER
//#define VAULTCHECK

namespace FileLocations
{
  std::string Anibox_Path;    //"Data/Anibox/";
  std::string Gizmo_Path;     //"Data/Gizmos/";
  std::string Image_Path;     //="Data/Images/";
  std::string Map_Path;       //="Data/Maps/";
  std::string Song_Path;      //="Data/Songs/";
  std::string Sound_Path;     //="Data/Sounds/";
}

#endif





switchmaster.h has all my #defines in it and stuff like that. My basic dependencies go like this: MapBox.h -> Gizbox.h -> Gizmo.h ->TexBox.h -> Picloader.h Where everything is dependent on the existence of Picloader.h Gizmo.h is also dependent on switchmaster.h so that is the only file that #includes it. I have a whole other separate project that includes this library. I compile this library, then I compile the other project. Now I decided to add that namespace at the bottom of switchmaster.h. I can't link anymore, I get this mess:
cd '/home/icecube/project5/debug/./src/editor' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" LC_MESSAGES="C" LC_CTYPE="C" gmake pr5edit
/bin/sh ../../libtool --tag=CXX --mode=link g++ -O0 -g3 -lGL -lSDL_mixer
-lGLU -lSDL_image -o pr5edit ice_edit.o keydown.o keygrabber.o mouseclick.o
movemouse.o navigate.o paintblocks.o paintfade.o paintgiz.o painthero.o
paintmap.o paintmaster.o paintmenu.o pr5edit.o superupdate.o ../../src/shared
/libicelibrary.a 
g++ -O0 -g3 -o pr5edit ice_edit.o keydown.o keygrabber.o mouseclick.o
movemouse.o navigate.o paintblocks.o paintfade.o paintgiz.o painthero.o
paintmap.o paintmaster.o paintmenu.o pr5edit.o superupdate.o /usr/lib/libGL.so
-L/usr/X11R6/lib /usr/lib/libSDL_mixer.so -L/usr/lib -lGLU /usr/lib
/libSDL_image.so -lpng /usr/lib/libtiff.so -ljpeg -lc -lz /usr/lib/libSDL.so
-lm /usr/lib/libXrandr.so /usr/lib/libXext.so /usr/lib/libXrender.so /usr/lib
/libX11.so /usr/lib/libxcb-xlib.so /usr/lib/libxcb.so /usr/lib/libXau.so
/usr/lib/libXdmcp.so -ldl -lvga -lpthread ../../src/shared/libicelibrary.a
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4
/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
keydown.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
keygrabber.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
keygrabber.o:/home/icecube/project5/src/editor/keygrabber.cpp:71: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
keygrabber.o:/home/icecube/project5/src/editor/keygrabber.cpp:71: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
keygrabber.o:/home/icecube/project5/src/editor/keygrabber.cpp:71: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
keygrabber.o:/home/icecube/project5/src/editor/keygrabber.cpp:69: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
keygrabber.o:/home/icecube/project5/src/editor/keygrabber.cpp:69: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
mouseclick.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
movemouse.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
movemouse.o:/home/icecube/project5/src/editor/movemouse.cpp:66: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
movemouse.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:187: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
movemouse.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:187: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
movemouse.o:/home/icecube/project5/src/editor/movemouse.cpp:65: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
movemouse.o:/home/icecube/project5/src/editor/movemouse.cpp:23: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
navigate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintblocks.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintfade.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintgiz.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
painthero.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmap.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmaster.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
paintmenu.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
pr5edit.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
superupdate.o:/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/home/icecube/project5/src/shared/Gizbox.cpp:23: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/home/icecube/project5/src/shared/Gizbox.cpp:25: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:187: multiple definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/home/icecube/project5/src/shared/Gizbox.cpp:26: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/home/icecube/project5/src/shared/Gizbox.cpp:26: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined here
../../src/shared/libicelibrary.a(Gizbox.o): In function `GizNode':
/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined here
../../src/shared/libicelibrary.a(Gizmo.o):/home/icecube/project5/src/shared/Gizmo.cpp:41: multiple definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizmo.o):/home/icecube/project5/src/shared/Gizmo.cpp:43: multiple definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined here
../../src/shared/libicelibrary.a(Gizmo.o):/usr/lib/gcc/i486-slackware-linux/4.2.4/../../../../include/c++/4.2.4/bits/basic_string.tcc:680: multiple
definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined
here
../../src/shared/libicelibrary.a(Gizmo.o):/home/icecube/project5/src/shared
/Gizmo.cpp:65: multiple definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined
here
../../src/shared/libicelibrary.a(Gizmo.o):/home/icecube/project5/src/shared
/Gizmo.cpp:84: multiple definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined 
here
../../src/shared/libicelibrary.a(Gizmo.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple 
definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple 
definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple 
definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple 
definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple 
definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple 
definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(Gizmoswitch.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:182: multiple 
definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/ios_base.h:123: multiple 
definition of `FileLocations::Anibox_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/ios_base.h:124: multiple 
definition of `FileLocations::Gizmo_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple 
definition of `FileLocations::Image_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:102: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/locale_facets.tcc:2562: multiple 
definition of `FileLocations::Map_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/ios_base.h:124: multiple 
definition of `FileLocations::Song_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:104: first defined 
here
../../src/shared/libicelibrary.a(MapBox.o):/usr/lib/gcc/i486-slackware-
linux/4.2.4/../../../../include/c++/4.2.4/bits/stl_algobase.h:188: multiple 
definition of `FileLocations::Sound_Path'
ice_edit.o:/home/icecube/project5/src/editor/ice_edit.cpp:106: first defined 
here
collect2: ld returned 1 exit status
gmake: *** [pr5edit] Error 1
*** Exited with status: 2 ***





I don't really understand it. I know if you declare a global variable in one .h file then you have to use the "extern" keyword in all the other .h files that depend on it. And I suppose FileLocations::Anibox_Path and all those are global variables so it's the same thing? But then why did the library compile just fine? I didn't put "extern" anything in Gizbox.h or MapBox.h and they both depend on Gizmo.h which depends on switchmaster.h. It's only when I try to compile the project that includes the library when I get all those multiple definition file errors. Why? I don't want any global variables anyway, should I even be using a namespace? Maybe I should change it to a struct and pass it everywhere? And how come things like std::string and std::cout and stuff don't give me linker errors? std is a namespace, right? Why don't #include <string> give me a crapload of multiple definition errors?

Share this post


Link to post
Share on other sites
What you are doing is basically creating a copy of those globals in every .cpp file that includes your .h file. You need to prefix each variable declaration with extern, then pick one .cpp file that actually defines the variables (i.e. matches the extern-ified declarations but without the extern qualifier). That way you will get exactly one copy of the global variables, and each file using your header will be able to access them via the namespace.

Share this post


Link to post
Share on other sites
uh... okay I put extern in front of each member variable in the namespace and now it all compiles. I guess it made them all "external" or something so now it doesn't try to make a new copy for every file that includes them? So now I have to define each one in some .cpp file's contructor I guess.

I guess it's impossible to make these member variables const then, huh? That's too bad, that's kind of what I wanted to do.

Share this post


Link to post
Share on other sites
Hey I tried it the namespace way and it still doesn't work. I put extern in front of everything:

#ifndef SWITCHMASTER
#define SWITCHMASTER

//day is x cycles long (x milliseconds on fast machine)
#define DAYLEN 1800000
//x cycles to shift daylight
#define DAYSHIFT 2000.0f
#define CLOUDSHIFT 2000.0f

#define HORZ 1024
#define VERT 768
#define BPP 32

//max length of a filename this program will work with (exluding extension, I think so really IFILE+4)
#define IFILE 10
//half the size of the hero you want to draw, make this obsolete eventually
#define IHEROHALF 58
#define DVAL 800

//#define RATSLAM 3
//#define RATDAMAGE 0.5f

//the number of defined gizmo types -1
//0 = standard player gizmo | NO AI | 32 PNG's, 1 GIZ
//1 = rat gizmo | SIMPLE AI | 16 PNG's
//2 = simple mill | SPIN | 3 PNG's
//3 = stain | | 4 PNG's
#define IGIZ 3
//number of defined anibox types -1
#define IANI 0
//the number of different possible layers -1
#define ILAYER 4

#define SCROLLSPEED 16.0f


//should only be three sections this effects
#define PROGRAMMER
//#define VAULTCHECK

//defined in MapBox.cpp constructor
//(only declaring one of these, in MapBox.h)
namespace Pathfinder
{
extern std::string Anibox_Path;//="Data/Anibox/";
extern std::string Gizmo_Path; //"Data/Gizmos/";
extern std::string Image_Path; //="Data/Images/";
extern std::string Map_Path; //="Data/Maps/";
extern std::string Song_Path; //="Data/Songs/";
extern std::string Sound_Path; //="Data/Sounds/";
};

#endif




And then in MapBox.cpp I did this:

MapBox::MapBox()
{
Pathfinder::Anibox_Path="Data/Anibox/";
Pathfinder::Gizmo_Path="Data/Gizmos/";
Pathfinder::Image_Path="Data/Images/";
Pathfinder::Map_Path="Data/Maps/";
Pathfinder::Song_Path="Data/Songs/";
Pathfinder::Sound_Path="Data/Sounds/";
}




The library with MapBox and everything compiled just fine again. But then when I tried to compile my editor which includes the library I got this:

cd '/home/icecube/project5/debug/./src/editor' && WANT_AUTOCONF_2_5="1"
WANT_AUTOMAKE_1_6="1" LC_MESSAGES="C" LC_CTYPE="C" gmake pr5edit
/bin/sh ../../libtool --tag=CXX --mode=link g++ -O0 -g3 -lGL -lSDL_mixer -lGLU
-lSDL_image -o pr5edit ice_edit.o keydown.o keygrabber.o mouseclick.o
movemouse.o navigate.o paintblocks.o paintfade.o paintgiz.o painthero.o
paintmap.o paintmaster.o paintmenu.o pr5edit.o superupdate.o ../../src/shared
/libicelibrary.a
g++ -O0 -g3 -o pr5edit ice_edit.o keydown.o keygrabber.o mouseclick.o
movemouse.o navigate.o paintblocks.o paintfade.o paintgiz.o painthero.o
paintmap.o paintmaster.o paintmenu.o pr5edit.o superupdate.o /usr/lib/libGL.so
-L/usr/X11R6/lib /usr/lib/libSDL_mixer.so -L/usr/lib -lGLU /usr/lib
/libSDL_image.so -lpng /usr/lib/libtiff.so -ljpeg -lc -lz /usr/lib/libSDL.so
-lm /usr/lib/libXrandr.so /usr/lib/libXext.so /usr/lib/libXrender.so /usr/lib
/libX11.so /usr/lib/libxcb-xlib.so /usr/lib/libxcb.so /usr/lib/libXau.so
/usr/lib/libXdmcp.so -ldl -lvga -lpthread ../../src/shared/libicelibrary.a
../../src/shared/libicelibrary.a(MapBox.o): In function `MapBox':
/home/icecube/project5/src/shared/MapBox.cpp:25: undefined reference to `Pathfinder::Anibox_Path'
/home/icecube/project5/src/shared/MapBox.cpp:26: undefined reference to `Pathfinder::Gizmo_Path'
/home/icecube/project5/src/shared/MapBox.cpp:27: undefined reference to `Pathfinder::Image_Path'
/home/icecube/project5/src/shared/MapBox.cpp:28: undefined reference to `Pathfinder::Map_Path'
/home/icecube/project5/src/shared/MapBox.cpp:29: undefined reference to `Pathfinder::Song_Path'
/home/icecube/project5/src/shared/MapBox.cpp:30: undefined reference to `Pathfinder::Sound_Path'
/home/icecube/project5/src/shared/MapBox.cpp:25: undefined reference to `Pathfinder::Anibox_Path'
/home/icecube/project5/src/shared/MapBox.cpp:26: undefined reference to `Pathfinder::Gizmo_Path'
/home/icecube/project5/src/shared/MapBox.cpp:27: undefined reference to `Pathfinder::Image_Path'
/home/icecube/project5/src/shared/MapBox.cpp:28: undefined reference to `Pathfinder::Map_Path'
/home/icecube/project5/src/shared/MapBox.cpp:29: undefined reference to `Pathfinder::Song_Path'
/home/icecube/project5/src/shared/MapBox.cpp:30: undefined reference to `Pathfinder::Sound_Path'
collect2: ld returned 1 exit status
gmake: *** [pr5edit] Error 1
*** Exited with status: 2 ***


Share this post


Link to post
Share on other sites
These are assignments, that is, just another function trying to use the globals.


//in header
extern std::string path;

//in some source file
std::string path;


Share this post


Link to post
Share on other sites
While generally dirty, sometimes generalities stay in the way of productivity. So generally try to avoid global variables and #define macros (e.g. will you always have a screen size of 1024x768x32). But that aside, I personally always have a file "globals.[cpp|cc|cxx|...]" that contains all definitions of your external variables, and which contains nothing and nothing but data. This avoids chaos in the future, when you want to change particular values.

So, go with something like (you don't have to #include the header with the external declarations):

globals.cpp

#include <string>
namespace FileLocations {
std::string Anibox_Path = "Data/Anibox/";
std::string Gizmo_Path = "Data/Gizmos/";
std::string Image_Path = "Data/Images/";
std::string Map_Path = "Data/Maps/";
std::string Song_Path = "Data/Songs/";
std::string Sound_Path = "Data/Sounds/";
}


And if you don't intend to ever change the paths during runtime, make them const:

globals.cpp
#include <string>
namespace FileLocations {
const std::string Anibox_Path = "Data/Anibox/";
const std::string Gizmo_Path = "Data/Gizmos/";
const std::string Image_Path = "Data/Images/";
const std::string Map_Path = "Data/Maps/";
const std::string Song_Path = "Data/Songs/";
const std::string Sound_Path = "Data/Sounds/";
}


file-locations.h
#include <string>
namespace FileLocations {
extern const std::string Anibox_Path;
extern const std::string Gizmo_Path;
extern const std::string Image_Path;
extern const std::string Map_Path;
extern const std::string Song_Path;
extern const std::string Sound_Path;
}


sidenote: Note that you also can underline your intent of variables being externally visible by writing like that:
namespace FileLocations {
extern const std::string Anibox_Path = "Data/Anibox/";
extern const std::string Gizmo_Path = "Data/Gizmos/";
[...]



Unfortunately, it doesn't prevent you from making errors like:
void f () {
extern const std::string s;
std::cout << s;
}
namespace { // <-- Anonymous namespace; the C++
// replacement of "static" at filescope.
extern const std::string s = "";
}

This seems to be syntactically correct, as per gcc 3.x (sorry, no 4.x on this box), but will fail due to an undefined to s.

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
If the namespace way is "dirty" is there a better way to do it?


Oh you misread my post, my fault. With "dirty", I meant that in objetc oriented languages you should generally avoid global variables; and in C++, you should try to avoid #define macros. Sometimes, they are the right thing (look at std::cout, which is a global variable), but in most cases not.

Using namespaces is actually a good technique to avoid name-pollution (look at PHP, which until 2008 lacked namespaces completely, and where you have thousands of functions with funky naming schemes like mysql_connect, just to avoid name-clashes).

Of course you could avoid using global variables by writing a class like that:

class Environment {
public:
std::string getAniboxPath () const { return "Data/Anibox/"; }
[...]
};

int main () {
Environment env;
std::cout << env.getAniboxPath() << std::endl;
}


If you use that, and somewhere in the future you decide to make those paths configurable (*), you are on the lucky side.


(*): Examplary code:


// Assuming "loadConfig()" to be a function to load values from some
// ini-file.

class Environment {
public:
Environment () : aniboxPath (loadConfig("anibox-path")) {
}
std::string getAniboxPath () const { return aniboxPath; }

private:
std::string aniboxPath;
[...]
};

int main () {
Environment env;
std::cout << env.getAniboxPath() << std::endl;
}

Share this post


Link to post
Share on other sites
Actually I was thinking it seemed dirty before you said so. I don't have any global variables but now I will with this namespace. And now I'll have to say extern everywhere, I don't like that. But I can only think of one other option. Define a class in a .h file and then declare an instance of it in a high level container, like MapBox, and pass it to all the lower containers.

...that's probably what I'll do. I mean I've gone this far without anything global, I don't really want to start now.

Share this post


Link to post
Share on other sites
First of all, all of your #defines are practically global variables. People don't frown upon global variables because they're global, but because they make assumptions that tend to force certain (bad) designs. #define's do the same thing, and certain defines can even cause some very unexpected behavior. So as far as wanting to avoid global variables simply because they're global and "you don't have any yet" isn't a very good reason, especially since you pretty much do have them.

Secondly, and more importantly, globals seem to be a decent solution for you here. Passing an instance of a class along to every function in your project is sloppy and not much better than globals (it could even be worse).

So, how to make it work?
Change the bottom of switchmaster.h to this:

namespace FileLocations
{
extern const std::string Anibox_Path; //"Data/Anibox/";
extern const std::string Gizmo_Path; //"Data/Gizmos/";
extern const std::string Image_Path; //="Data/Images/";
extern const std::string Map_Path; //="Data/Maps/";
extern const std::string Song_Path; //="Data/Songs/";
extern const std::string Sound_Path; //="Data/Sounds/";
}



Make a file switchmaster.cpp with this content:

namespace FileLocations
{
const std::string Anibox_Path("Data/Anibox/");
const std::string Gizmo_Path("Data/Gizmos/");
const std::string Image_Path("Data/Images/");
const std::string Map_Path("Data/Maps/");
const std::string Song_Path("Data/Songs/");
const std::string Sound_Path("Data/Sounds/");
}



In any other code, do the following:

#include "switchmaster.h"

void someFunction()
{
// do some stuff
loadMap(FileLocations::Map_Path + "mapname.map");
}



Notice that the extern keyword only appears in 1 place, the .h file declaration.
Notice that the variables can be const.
Notice that this is better than the "pass an instance of a class" method because a) someFunction() can run without depending on another function to inistialize that class for it, b) it's a lot clearer what's happening (i.e. you're grabbing the global Map_Path variable, not some Map_Path member of an object which could have multiple instances in the project - maybe that Map_Path is just for that object? who knows?

Hope this clears up what everyone else was trying to show you.

Share this post


Link to post
Share on other sites
Yeah that works great.

I used to have about 50 global variables with "extern" all over the place. As I learned more I got rid of all that. I'll be getting rid of most of those #defines, too.

I don't think I'll be getting rid of this, though:
#define IGIZ 3

I figure whenever I make up a new kind of monster I can just increase IGIZ to 4, then 5, etc. I have to program a new AI and define how many sprites it gets and how it's drawn. Then I increase IGIZ and the number is correct throughout the program.

Is that a bad idea? I don't see anything wrong with it. I don't see any other way to do it. If someone using the editor wants to use some sprites to create a new monster he has to give it one of those types and valid types are 0-IGIZ througout the program.

Share this post


Link to post
Share on other sites
Quote:
Original post by c0uchm0nster
...


Err, but you see that you basically copied my post with some added salt?

The only difference being that you explicitly don't recommend to use an instance of some class that is passed through (whereas I implicitly didn't recommend that inside my code-sample).


Quote:
Passing an instance of a class along to every function in your project is sloppy and not much better than globals (it could even be worse).


No, it is better than global variable, because you can limit visiblity before nodes higher in the call-tree and before nodes deeper in the call-tree. E.g., say your call-tree is [<program-scope>, <filescope>, x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7]. Then you can make it so that only [x_2..x_4] can see and use a particular instance of some class, but [<program-scope>..x_1] and [x_5..x7] can't neither. You can even make it so that only x_2 has random-access to your instance, but [x_3..x_4] only get read-access. You can even control when your instance is constructed, and when it is destructed, with global variables, you can not.


Anyways, that is not what I recommended. I recommended (I know it wasn't your idea to pass them through, just clarifying):
class Environment {
public:
std::string aniboxPath () const { return "Data/Anibox/"; }
[...]
};

int main () {
Environment env;
std::cout << env.aniboxPath() << std::endl;
}


It's not a global variable, it's not passed through, it's not a singleton, it does not contain any static methods, and with an increasing number of paths, adding new values is even less typing work than writing redundant pairs of declarations and definitions (*). And the function main() will work fine in the future when you rewrite the exemplary class Environment to something like:

class Environment {
public:
Environment () : aniboxPath (loadConfig("anibox-path")) {
}
std::string aniboxPath () const { return aniboxPath; }

private:
std::string aniboxPath;
[...]
};


Also, you can anyhow declare a global instance of that class, if you must, and still be future safe to some degree (if you ever intend to make such paths configurable some day, you could run into static initialization order fiasco).

Btw, using a surrounding class, your code became (unclean):
#include "switchmaster.h"
void someFunction()
{
// do some stuff
loadMap(fileLocations.Map_Path() + "mapname.map");
}

Not less clear, and just one additional char. Plus, sorry, you are future safe to some degree.

Anyhow, I have nothing against globals in that case actually, but my intention was a bit blurred. Though a class doesn't cost more than a namespace, in any respect. But you get some benefits (no, YAGNI arguments are not applicable, as wee see at (*)).


Quote:
Notice that the extern keyword only appears in 1 place, the .h file declaration.

It is still perfectly allowed to put the extern keyword upon definitions, which will contribute to self-documenting code (you clarify that you didn't intend to make that variable file-global, but program-global).

Quote:
Notice that the variables can be const.

Actually, they should be const.



(*)
(p-code with a handful of paths)
class Environment {
public:
std::string aniboxPath () const { return "Data/Anibox/"; }
std::string aniboxPath () const { return "Data/Anibox/"; }
std::string aniboxPath () const { return "Data/Anibox/"; }
std::string aniboxPath () const { return "Data/Anibox/"; }
};


vs.

namespace Environment {
std::string const aniboxPath = "Data/Anibox/";
std::string const aniboxPath = "Data/Anibox/";
std::string const aniboxPath = "Data/Anibox/";
std::string const aniboxPath = "Data/Anibox/";
}
namespace Environment {
extern std::string const aniboxPath;
extern std::string const aniboxPath;
extern std::string const aniboxPath;
extern std::string const aniboxPath;
}





edit
sidenote: You could also make the member functions static, or non-member inline functions.

[Edited by - phresnel on April 30, 2009 5:04:58 AM]

Share this post


Link to post
Share on other sites
Hey Phresnel, you define the Environment class above main(). You declare an instance of it in main() called env. Don't you have to pass env to everything that needs those paths?

And if there's a class defined in another file, it's going to look like this:

class Whatever
{
void FunctionThatNeedsPathInfo(Environment &env);
};



And now that file won't compile because Environment isn't defined. Were you just typing a general solution and I was supposed to figure that out or am I misunderstanding something?

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
Hey Phresnel, you define the Environment class above main(). You declare an instance of it in main() called env. Don't you have to pass env to everything that needs those paths?

You can do one of those:
0) You pass it through (maybe as const-reference, i.e. as read-only)
1) Everyone who needs the information declares an own, local instance
2) You create a global instance


Quote:

And if there's a class defined in another file, it's going to look like this:


class Whatever
{
void FunctionThatNeedsPathInfo(Environment &env);
};



There are two solutions:


class Environment;
class Whatever
{
void FunctionThatNeedsPathInfo(Environment &env);
};

Which works because you pass by reference. But you will have to #include the whole definition of Environment before the actual definition "void FunctionThatNeedsPathInfo(Environment &env) { .... }".

Or, better, just #include the proper file:
#include "environment.h"
class Whatever
{
void FunctionThatNeedsPathInfo(Environment &env);
};


Of course, environment.h was only before function main() for the sake of example. So put it into a header-file (like your extern declaration previously), but don't forget include guards (#ifdef XXX ... #define XXX ... #endif // XXX).[/quote]


Quote:
Were you just typing a general solution and I was supposed to figure that out or am I misunderstanding something?

Yes.
;)

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
Err, but you see that you basically copied my post with some added salt?


Yeah, that's why I said that that's what I was doing at the bottom of the message. He didn't understand previous explanations so I thought I'd elaborate, and lo and behold he then understood.

As for a class vs globals I guess we'll just have to agree to disagree on this implementation. Nevertheless hardcoding these values and only allowing one value for each isn't "best" either IMHO, but I didn't want to muddy a simple compile problem topic with design pattern arguments. =p

No reason to bite my head off about it, just trying to help.

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