Thread: Structure in header won't declare.

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    8

    Structure in header won't declare.

    I'm trying to create a game for my own entertainement, and for some reason, it gives me an error.

    I'm posting the entire thing so far.

    Main.cpp
    Code:
    //Inculde the main header file
    #include "main.h"
    //Incude the global variable allocation header
    #include "globalvar.h"
    
    //Game initialization function. Gets the game ready to run.
    int functMainStartGame()
    {
        global.exitgame = false;
    }
    
    //Step function, for the main program. All main program code executed here.
    int functMainStep()
    {
        //while(global.exitgame == false)
        //{
        ////put code for game here.
        //}
        
    }
    
    //Game ending function. Ends classes, closes rendering system, ect.
    int functMainEndGame()
    {
    }
    
    int main()
          {
          functMainStartGame();
          functMainStep();
          functMainEndGame();
          return 0;
          }
    main.h
    Code:
    //todo: Add header files to declare here.
    
    //create global variable container.
    
    struct globalStruct
    {
          int exitgame;
          int *entities[0];
          int numbofentities;
    };
    extern globalStruct global;//declare it


    And globalvar.h
    Code:
    globalStruct global;
    EDIT: Oh, entites.cpp too.
    Code:
    #include "globalvar.h"
    
    class entMain
    {
          public:
                int myx;
                int myy;
                float xspeed;
                float yspeed;
          entMain(int x, int y)
          {
          global.instances[global.numbofentities] = &this;
          global.numbofentities += 1;
          }
          ~entMain()
          {
          }
          int entRepeatMe()
          {
          }
    };
    http://img170.imageshack.us/img170/9614/whatdk4.png

    I'm,using Dev C++ v4.9.9.2.

    Please note I am just beginning at C++, so it may have been something simple.

    Thanks

    -CornJer

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It helps to post the exact error and the line on which it occured; however, I can guess from your title what the error is.

    What's this supposed to do? Is it just a template?
    Code:
    int *entities[0];
    Are you getting a linker error to the effect that you have two variables of the same name?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    8

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, entries.cpp includes globalvar.h but not main.h; it tries to declare a globalStruct without knowing what a globalStruct is. You need to have the definition of the structure in entries.cpp.

    [edit]
    Think about it; after the preprocessor does its job, entries.cpp looks like this:
    Code:
    // What's a globalStruct?
    globalStruct global; // was #include "globalvar.h"
    
    class entMain
    {
          public:
    You were very thourough in your question, by the way. I'm impressed.
    [/edit]
    Last edited by dwks; 10-21-2006 at 09:28 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Thanks!

    But now it gives me this error as well:

    http://img135.imageshack.us/img135/1672/error2rk2.png

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    globalStruct doesn't appear to have an instances member; perhaps you meant entities?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
          global.instances[global.numbofentities] = &this;
    That line... first: global has no member named instances. The closest thing is probably entities. Second, (the error) is that you are saying "&this" - take the address of 'this'. (I cannot imagine what the answer would be, can you?) The variable 'this' is a pointer to the class who's member is being called, and is (in this case) of type entMain *.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Yeah, sorry, meant entities.

    So if the keyword "this" doesn't point to an instance of class's address, what does?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It does point to the instance of a class; but it's a pointer already. &this will give you **class_type.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Odd. If thats so, why does it give me this error?

    Code:
    global.entities[global.numbofentities] = this;
    http://img100.imageshack.us/img100/760/error3ti9.png

    Sorry to keep pestering you.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You're trying to assign a class to an
    Code:
    int *entities[0];
    Not good. Declare it like so:
    Code:
    entMain *entities[whatever];
    You may need a forward declaration before the structure:
    Code:
    class entMain;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    8
    Excellent! It runs perfectly now!

    Thanks a bundle!

  13. #13
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    As a note, making an array of pointers to objects is not very good practice, for 2 reasons:

    1. No way to automatically deallocate the objects,
    2. No way to automatically allocate/deallocate the array.

    I'd say you should consider a boost:tr_vector<> for this job. It will automatically call destructors for your objects as they're removed, and it will automatically call destructors when your program ends.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You do know you can copy and paste errors from dev-c++, you don't have to keep posting images.
    Just select the "Compile Log" tab and you can just select the text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Header File Understanding Help
    By hern in forum C Programming
    Replies: 1
    Last Post: 04-16-2004, 09:58 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM