Archived

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

Zyroth

Ok... I'm stumped

Recommended Posts

Ok, I''ve included my own header file Sound.h in two cpp files. In the header file I''ve defined SOUND_H and when I go to include them I use #ifndef SOUND_H #include "Sound.h" #endif But it''s not working, its giving me build errors because thing are of course alreasy defined. I already checked the spelling and that I defined them properly. What am I doing wrong here?

Share this post


Link to post
Share on other sites
Hi, in .cpp file simply include de .h file:

#include "Sound.h"

and in the Sound.h do this:

#ifndef SOUND_H
#define SOUND_H

// File code...

#endif

That''s it. It should work correctly.
Hope this helps.

Bye

Share this post


Link to post
Share on other sites
Inside sound.h itself put:

#ifndef SOUND_H
#define SOUND_H

blah blah blah

#endif

..........

Then in your source files, just include "sound.h"

See if that helps.

EDIT: Whoa, lots of replies there!

[edited by - Waverider on October 2, 2002 12:34:10 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, I just tried #pragma once with gcc and got a warning saying it''s obsolete... So... yeah...

Share this post


Link to post
Share on other sites
I tried putting my code in the #ifndef but it still doesn''t work.

These are the errors that I''m getting:

Linking...
Sound.obj : error LNK2005: "struct _DSBUFFERDESC dsbd" (?dsbd@@3U_DSBUFFERDESC@@A) already defined in main.obj
Sound.obj : error LNK2005: "struct tWAVEFORMATEX pcmwf" (?pcmwf@@3UtWAVEFORMATEX@@A) already defined in main.obj
Sound.obj : error LNK2005: "struct pcm_sound_typ * sound_fx" (?sound_fx@@3PAUpcm_sound_typ@@A) already defined in main.obj
Sound.obj : error LNK2005: "struct IDirectSound * lpds" (?lpds@@3PAUIDirectSound@@A) already defined in main.obj
Debug/builder.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

builder.exe - 5 error(s), 0 warning(s)

Any Ideas...

Share this post


Link to post
Share on other sites
Did you put the #define in after the ifndef?

Think about what you are trying to do here - if it isn''t defined, then include the file and define it, if its already defined, then ignore everything.

Share this post


Link to post
Share on other sites
yeah my code pretty much goes:

#ifndef WINDOWS_H
#include <windows.h>
#endif

#ifndef MMSYSTEM_H
#include <mmsystem.h>
#endif

#ifndef DSOUND_H
#include <dsound.h>
#endif

#ifndef SOUND_H
#define SOUND_H

#define DSVOLUME_TO_DB( volume ) ( ( DWORD )( -30*( 100 - volume ) ) )
#define MAX_SOUNDS 256

#define DSBCAPS_CTRLDEFAULT DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLFREQUENCY

// digital sound object state defines
#define SOUND_NULL 0 // " "
#define SOUND_LOADED 1
#define SOUND_PLAYING 2
#define SOUND_STOPPED 3

struct pcm_sound_typ
{
LPDIRECTSOUNDBUFFER dsbuffer;
int state;
int rate;
int size;
int id;
};

pcm_sound_typ sound_fx[ MAX_SOUNDS ];

LPDIRECTSOUND lpds;
DSBUFFERDESC dsbd;
WAVEFORMATEX pcmwf;

bool InitDS( HWND hwnd );
bool PlayDS( bool loopSound, int id );
bool StopDS( int id );
bool SetVolumeDS( int vol, int id );
bool SetFrequencyDS( int freq, int id );
bool SetPanDS( int pan, int id );
bool DeInitDS();
int Load_WAV( char* filename, int CONTROL_FLAGS = DSBCAPS_CTRLDEFAULT );

#endif

[edited by - zyroth on October 2, 2002 4:45:21 PM]

Share this post


Link to post
Share on other sites
my guess is that your Sound.h isn't just definitions but it is also code. you should be splitting everything into a .h AND a .cpp

example:

Sound.h:

  
#ifndef SOUND_H
#define SOUND_H

class Sound {
protected:
int whatChoooGot;
public:
void doSomeStuff(int yourMom);
};

#endif




Sound.cpp:

  
#include "Sound.h"

void Sound::doSomeStuff(int yourMom) {
for (int i = 0; i < yourMom; i++)
cout << "is hot\n";
}


make sense?

[edited by - Palidine on October 2, 2002 4:48:13 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Zyroth
yeah my code pretty much goes:

#ifndef WINDOWS_H
#include <windows.h>


#ifndef MMSYSTEM_H
#include <mmsystem.h>


#ifndef DSOUND_H
#include <dsound.h>


#ifndef SOUND_H
#define SOUND_H

#define DSVOLUME_TO_DB( volume ) ( ( DWORD )( -30*( 100 - volume ) ) )
#define MAX_SOUNDS 256

#define DSBCAPS_CTRLDEFAULT DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLFREQUENCY

// digital sound object state defines
#define SOUND_NULL 0 // " "
#define SOUND_LOADED 1
#define SOUND_PLAYING 2
#define SOUND_STOPPED 3

struct pcm_sound_typ
{
LPDIRECTSOUNDBUFFER dsbuffer;
int state;
int rate;
int size;
int id;
};

pcm_sound_typ sound_fx[ MAX_SOUNDS ];

LPDIRECTSOUND lpds;
DSBUFFERDESC dsbd;
WAVEFORMATEX pcmwf;

bool InitDS( HWND hwnd );
bool PlayDS( bool loopSound, int id );
bool StopDS( int id );
bool SetVolumeDS( int vol, int id );
bool SetFrequencyDS( int freq, int id );
bool SetPanDS( int pan, int id );
bool DeInitDS();
int Load_WAV( char* filename, int CONTROL_FLAGS = DSBCAPS_CTRLDEFAULT );

#endif // for sound.h

#endif // for dsound.h

#endif // for mmsystem.h

#endif // for windows.h

[edited by - zyroth on October 2, 2002 4:45:21 PM]



try it this way and see if it works.
as you can see the "endif"s belong at the very bottom of the code, not right under the ifndef and define. this rule applies whether you have one or a million.

hope this helps

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why.... *hmm* whyyy would you #ifndef/#endif around each #include? The header should (if I''m not mistaken) already
to that for you! I think that is the whole point... *~*

You may also want to make any variables in the .h file extern

#ifndef SOUND_H // is this used elsewhere?!
#define SOUND_H
#include <windows.h> // inside: #ifndef, ETC.

extern pcm_sound_typ sound_fx[ MAX_SOUNDS ];
extern LPDIRECTSOUND lpds;
extern DSBUFFERDESC dsbd;
extern WAVEFORMATEX pcmwf;
// ...
#endif


// in .cpp/.cc/.whatever

// other headers
#include "sound.h"

pcm_sound_typ sound_fx[ MAX_SOUNDS ]; // extern!!
LPDIRECTSOUND lpds;
DSBUFFERDESC dsbd;
WAVEFORMATEX pcmwf;


// continue w/ implementation..


Any questions?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
What are you yelling about?


ok... #ifndef SOUND_H is (I believe) ''global like''... so if you
have another file using SOUND_H as it''s ''bounds'' then one of
the two files will NOT be included.

I was saying, don''t put #ifndef''s around the #include''s in
the .h, or .cpp... it should be done for you in the .h you are
including.

EX:

// sound.h
#ifndef SOUND_H
#define SOUND_H
// ... code stuffs
#endif


// some cpp file
#ifndef SOUND_H
#include "sound.h"
#endif


It is redundant... a waste time. Any more questions
(heh, if that was even directed at/toward me)

Hope this helps..

:D ~z~ :D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes. hehe. I think ''extern''ing all the variables and using
the #ifndef''s correctly, he will have no problem (I hope) (assuming ''he'')


:D ~z~ :D

Share this post


Link to post
Share on other sites
quote:

my guess is that your Sound.h isn''t just definitions but it is also code. you should be splitting everything into a .h AND a .cpp



No he shouldn''t. Keeping smaller methods in the .h file will inline them, which is advantageous. Larger methods should always be implemented in the .cpp file however.

Share this post


Link to post
Share on other sites