Hi,


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
{
public:
	MenuClass();
};


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


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


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

(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?


Thanks,
Ben

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.
Add:

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
...

commented: Helpful +0

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,
Ben

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.