Advertisement

loading bitmaps

Started by February 24, 2004 03:04 PM
4 comments, last by NickKirt 21 years ago
HI, I''ve got a problem, I have a bitmap.h file that loads a bitmap and returns a pointer to the bitmap. It''s from the opengl game programming book. Problem is this, I have a class trail. it uses bitmap.h to load a bitmap but i also have a class draw that is my main drawing page that also needs to load a bitmap. Problem is that i get error messages saying "one or more multiply defined symbols found" But if i get rid of the import "bitmap.h" in trail. it says that it does not know *LoadBitmapFile! can''t i import a same .h file twice? any help is appreciated nick
No, you can not load the same .h file two times. Once a .h file is loaded, it can be accessed by most of the rest of your code. The safe way to handle .h files is by wrapping them in #ifdef statements.

For example, in your .h file, make the first line look like:
#ifndef _SOME_UNIQUE_NAME_HERE_H
#define _SOME_UNIQUE_NAME_HERE_H

and then make the last line of the .h file be:
#endif

these are precompilier commands that tell the compilier if the symbol isn''t known (ie: the file hasn''t been loaded) then define the symbol and load the file.

Black holes are where God divided by 0.
Advertisement
Ok, i already had the if statement in the h file. i still have a problem.

Ok so you can only include an h file once.
BUT i still need the functions that are described in bitmap.h.(load bitmap functions) now i could just copy paste it i suppose. but surely there must be a way of being able to define a function once and use it anywhere.

and why can a .h file be used only once?

thx again.
nick
maybe i should put it another way.

how do i, using Nehe''s latest base code. include an h file. that can be used anywhere, even if i add extra classes. So i want Example.cpp to be able to use the .h file and extra classes that i define.

thx
nick
There can only be at most one declaration of an object in each translation unit. There can only be one definition of an object in the entire program. Your program should look something like:
Bitmap.h:
#if !defined(MY_BITMAP_HEADER)	#define MY_BITMAP_HEADERclass Bitmap{	Bitmap(); // example constructor	// more member functions and variables};#endif 

Bitmap.cpp:
#if !defined(MY_BITMAP)	#define MY_BITMAP	#include "Bitmap.h"Bitmap::Bitmap(){	// code}// more member functions#endif 

Trail.h:
#if !defined(MY_TRAIL_HEADER)	#define MY_TRAIL_HEADER	#include "Bitmap.h" // if neededclass Trail{	// member functions and variables};#endif 

Trail.cpp:
#if !defined(MY_TRAIL)	#define MY_TRAIL	#include "Trail.h"	#include "Bitmap.h" // only if needed here but not needed in trail.h// member functions#endif 

Draw.h:
#if !defined(MY_DRAW_HEADER)	#define MY_DRAW_HEADER	#include "Bitmap.h" // if neededclass Draw{	// member functions and variables};#endif 

Draw.cpp:
#if !defined(MY_DRAW)	#define MY_DRAW	#include "Draw.h"	#include "Bitmap.h" // only if needed here but not needed in draw.h// member functions#endif 


And then compile Bitmap.cpp, Trail.cpp and Draw.cpp, along with any other cpp files you have.

Enigma
thx a lot, will try.

where can i find information about this type of thing, cos i''ve had a lot of problems with include stuff before.

thx
nick

This topic is closed to new replies.

Advertisement