Jump to content
  • Advertisement
Sign in to follow this  
orcfan32

Redefinition without Redefinition - GLOBALS

This topic is 4657 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

Log:
In file included from C:\CPP\SS\Main.cpp:7:
C:\CPP\SS\/Globals.h:2: error: redefinition of `struct SoundObj'
C:\CPP\SS\/Globals.h:2: error: previous definition of `struct SoundObj'
C:\CPP\SS\/Globals.h:9: error: redefinition of `SoundObj SoundSys'
C:\CPP\SS\/Loader.cpp:10: error: `SoundObj SoundSys' previously declared here

Execution terminated
Problem: This is saying that it's redefined where it is first defined... Main.cpp
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <math.h>
#include "Loader.cpp"
#include "Globals.h"

extern SoundObj SoundSys;
Loader.cpp
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
#include <math.h>
#include "Globals.h"

extern SoundObj SoundSys;
Globals.h
struct SoundObj
{
     DWORD FreqMHz[2000];
     DWORD Length[2000];
     int TotalNotes;
     int CurrentNote;
};

SoundObj SoundSys;
Using Windows XP and Dev-C++.

Share this post


Link to post
Share on other sites
Advertisement
Also don't include cpp files. That's just nasty.

Oh and put the declarations in your header between #ifndef BLAH #define BLAH ... #endif so the code in your header won't be included more than once in one file.
Eample:

test.h:


#ifndef __TEST_H__
#define __TEST_H__

// Put decls here...

#endif



-SirKnight

Share this post


Link to post
Share on other sites
Because cpp files are SOURCE FILES and h files are HEADER FILES. Header files are designed to be included, source files are not! Just b/c it works doesn't mean it's a good idea. Including cpp files just leads to very poor code design. Trust me, I've done this long enough to know better. :)



-SirKnight

Share this post


Link to post
Share on other sites
Ok so you have a header file that you declare some function in. Then you make a cpp file that includes this header and defines this function's implementation. Then you have your main cpp which includes ONLY that header. Now you can use this function in your main cpp. There is no need to include the cpp file, the linker makes sure that any call to your function gets properly linked to the implementation, which in this case was in it's own cpp file.

Eample (with a global too):

test.h

#ifndef __TEST_H__
#define __TEST_H__

extern int myglobal;

void func();

#endif



test.cpp

#include "test.h"

int myglobal = 0;

void func()
{
DoSomething....
}



main.cpp

#include "test.h"

int main()
{
func();

cout << myglobal << endl;

return 0;
}




Make sense now?


-SirKnight

Share this post


Link to post
Share on other sites
Yeah that is a good read and any author who doesn't talk about that exact stuff in their Learn C/C++ book needs to be kicked. ;)



-SirKnight

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!