Beginner C++ problem....

L

Lee Garrington

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
 
V

Victor Bazarov

Lee Garrington said:
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
 
R

Rolf Magnus

Lee said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top