Quick C++ Question

Miscellaneous Forums/General Discussion/Quick C++ Question

Haven't done any C++ for a while, and to be honest I mainly used standard C functions with all code in one big file. I'm now using classes and multiple files.

I've defined each class in a header file, a class function file for each class, and I have a main.cpp file. I define the class objects in the main.cpp file.

Now the problem is, because I'm not defining the class object until main, one of my classes can't access the class function of another. How do I accomplish this?

Thanks in advance, Steve.

Classes

csBoard
csInput

Objects

obBoard
obInput

Problem lines in csInput

iMovepiece = obBoard.iBoard[iSqr];
obBoard.update(iMovepiece, iMovefrom, iMoveto);

Clearly class object obBoard hasn't been defined yet (just the class definition) so gives a compiler error. It doesn't work with the class name csBoard either.

Main File

csBoard obBoard;
csInput obInput;

can't you extern the reference to obBoard in the file/header that you are using it?

maybe something along the lines of :
extern csBoard obBoard;


Although one might surmise that your design is flawed if you need to refer to a class instance that is at the top of your hierarchy.

But I ain't no expert... so.. ;-)

Have you tried forward declaration?
That is putting "class csBoard ;" in your csInput header file, before the csInput class.

Yeah - tried all that.

Ok using this code now compiles. :-)

#ifndef INPUT_H
#define INPUT_H

	class csInput {
		int iMove, iMovepiece, iMovefrom, iMoveto;
		int iSqr, iConvertcol, iConvertrow;
		char cCol, cRow;
		bool bError, bPlaygame;

		public:
			csInput();
			~csInput();
			bool getmove(csBoard obBoard);
			void draw();
	};

#endif


bPlaygame = obInput.getmove(obBoard);


Are you including the csBoard header file in your csInput header?

No I'm passing the obBoard object to the obInput object.

Upload all your source code and let me look at it.

Thanks for the offer Noel, having a re-think on my implementation at the moment.