Monday, May 24, 2010

How avoid cyclic dependency error while declaring header files?

I did a small program in C++. For eg: I have two files Client.cpp and Server.cpp. I've included header file Client.h in Server.cpp. But i'm in need to access a method defined in Server.db say GetServerName() from Client.cpp. If i call by Sever Class object like server.GetServerName() by declaring Server.h in Client.cpp, I get error. I realize i'm declaring both .h files in each other. This causes error I know. Then how to avoid this error without setting the return value of GetServerName() from Server.cpp into Client.cpp. Hope u get my point. Any ideas... pleas.

How avoid cyclic dependency error while declaring header files?
?





Did you use include guards in your header files?


e.g.





#ifndef __SERVER_H_


#define __SERVER_H_





... // class definition goes here.





#endif











Otherwise, if there are such strong dependencies between the server and client then why don't you define the class definitions in one file (e.g. Network.h), and then define the actual implementation in Client.cpp and Server.cpp?





Why does the client have a reference to a server object that exists in the programs local memory space? Servers and client should be running as separate processes.


No comments:

Post a Comment