Beginner C++ problem....

Discussion in 'C++' started by Lee Garrington, Oct 29, 2003.

  1. Hey,

    Recently I decided to learn C++ so that I could port over one of my Java
    programs to make it faster. Basically everything has ported over fine so
    far until I came up against the following problem....

    The problem involves 2 classes. A Game class and a Solver class. The Game
    class updates the game board etc. and the Solver class is called upon when
    it needs to solve the board. To do this I have an instance of Solver
    declared in Game so that Game can start it when needed and I also have an
    instance of Game in Solver so that the solver can call methods in Game to
    update the board throughout the solve. I simply cant get it to work though.
    I have #ifndef'ed both the .h files but they just cant #include each other
    without erroring. This is done no problem in java.

    Surely there is a way to do what I want in C++. If there is can someone
    please tell me how?

    Much appreciated.
    Lee
     
    Lee Garrington, Oct 29, 2003
    #1
    1. Advertising

  2. "Lee Garrington" <> wrote...
    > Recently I decided to learn C++ so that I could port over one of my Java
    > programs to make it faster. Basically everything has ported over fine so
    > far until I came up against the following problem....
    >
    > The problem involves 2 classes. A Game class and a Solver class. The

    Game
    > class updates the game board etc. and the Solver class is called upon when
    > it needs to solve the board. To do this I have an instance of Solver
    > declared in Game so that Game can start it when needed and I also have an
    > instance of Game in Solver so that the solver can call methods in Game to
    > update the board throughout the solve. I simply cant get it to work

    though.
    > I have #ifndef'ed both the .h files but they just cant #include each other
    > without erroring. This is done no problem in java.
    >
    > Surely there is a way to do what I want in C++. If there is can someone
    > please tell me how?


    Look up "forward declaration" in the FAQ or on Google.

    Victor
     
    Victor Bazarov, Oct 29, 2003
    #2
    1. Advertising

  3. Lee Garrington

    Rolf Magnus Guest

    Lee Garrington wrote:

    > Hey,
    >
    > Recently I decided to learn C++ so that I could port over one of my
    > Java
    > programs to make it faster. Basically everything has ported over fine
    > so far until I came up against the following problem....
    >
    > The problem involves 2 classes. A Game class and a Solver class. The
    > Game class updates the game board etc. and the Solver class is called
    > upon when
    > it needs to solve the board. To do this I have an instance of Solver
    > declared in Game so that Game can start it when needed and I also have
    > an instance of Game in Solver so that the solver can call methods in
    > Game to update the board throughout the solve.


    That is an endless loop. Your Game contains a Solver, which contains a
    Game, which contains a Solver, which....
    You'd need an infinite amount of memory for it. One of your classes must
    contain a pointer or reference to the other instead of an instance.

    > I simply cant get it to work
    > though. I have #ifndef'ed both the .h files but they just cant
    > #include each other without erroring.


    Of course not. If you #include the solver somewhere, that would start by
    #including the Game header, which in turn starts by #including the
    solver header.... again an endless loop.

    > This is done no problem in java.


    Java is very different from C++.

    > Surely there is a way to do what I want in C++. If there is can
    > someone please tell me how?


    Ever had a look into the FAQ to this group? You can find it at:

    http://www.parashift.com/c -faq-lite/

    You might be interested in question 38.11.
     
    Rolf Magnus, Oct 30, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry Smith

    Trivial resources problem (beginner)

    Larry Smith, Oct 2, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    526
    Scott Manson
    Oct 2, 2003
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    621
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Rensjuh
    Replies:
    7
    Views:
    1,008
    Mabden
    Sep 2, 2004
  4. Toni
    Replies:
    3
    Views:
    2,263
    msnews.microsoft.com
    Jul 29, 2006
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    235
    7stud --
    Apr 12, 2011
Loading...

Share This Page