I am working on a piece of OOP which needs to be ran entirely by classes, by that meaning the main function should only create the first class and the rest of the program ran by member functions of various classes. Part of this task requires me to keep encapsulation. So all varible members need to be private.

The problem with this is that I am trying to send infomation to other classes using call by reference. See the example below:

class MenuClass

class InputClass // Objects : DataIn and Filter
	void 	LoadState(SavedClass &Save, long SavedLength, bool type); 	// References "Save"

class OutputClass // Objects : DataOut
	int	ApplyFilter(InputClass &DataIn,InputClass &Filter);		// References DataIn and Filter

class SavedClass  // Objects : Save
	void Save(InputClass &DataIn,InputClass &Filter) const;		// References DataIn and Filter

(Showing just the relevent code)

Now the problem here is that I get the errors:

error C2061: syntax error : identifier 'SavedClass'
error C2660: 'InputClass::LoadState' : function does not take 3 arguments
error C2511: 'void InputClass::LoadState(Saved &,long,bool)' : overloaded member function not found in 'InputClass'

Is there a better way of doing this?


The first error comes from you having SavedClass as a parameter of

void 	LoadState(SavedClass &Save, long SavedLength, bool type);

You might be able to get rid of this error by forward declaring class SavedClass.

class SavedClass;

before the definition of class InputClass and this error will probably disappear.
The third Error is likely to be a typo: Saved instead of SavedClass.

Mind you: In general it is not a good idea to force everything into classes. There are many instances where this leads to bad code. It's ok as a homework-project to force you to think about encapsulation, but in real-life applications you should use the whole spectrum of tools that C++ offers you:
- templates
- metaprogramming
- preprocessor instructions (use *VERY* sparingly!!! There usually is a much better alternative to #defines!! But for certain things like debug/build instructions they are useful)
- members and non-members/operators of a class


That seems to have done the trick, works perfectly I wasn't aware that you could declare the class before defining it.

Just as info, all errors were as a result of not declaring the class for the compiler before it was used. The syntax was invalid due to me copy and pasting in bits.

Thanks alot,

Edited 5 Years Ago by BenjyC: n/a

This question has already been answered. Start a new discussion instead.