Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!nic.csu.net!csus.edu!decwrl!deccrl!bloom-beacon!snorkelwacker.mit.edu!linus!linus!maestro!chaloux From: chaloux@maestro.mitre.org (Dave Chaloux) Newsgroups: comp.lang.c++ Subject: Message-ID: <1991May28.162856.12685@linus.mitre.org> Date: 28 May 91 16:28:56 GMT Sender: news@linus.mitre.org (News Service) Reply-To: chaloux@maestro.mitre.org (Dave Chaloux) Organization: The MITRE Corporation, McLean, Va Lines: 37 Nntp-Posting-Host: maestro.mitre.org I am new to C++ and have come across the following problem. Say that you are building a communications program and you have two different kinds of ports you must read and two kinds of messages that are naturally associated with each port. In otherwords you have a Port_A class and a Port_B class. They read and write respectively Message_A class objects and Message_B class objects. Now say that you want to write your program such that you can write a message to a port without knowing the kind of port or the kind of message. No problem. Have a base Message class and a base Port class. Have virtual functions for the Port class that write Message_A and Message_B type objects. Now have a write virtual function for class Message that takes as an argument an object of type Port. Now we might write the message as follows: message->write(&port_object) Since a class derived from a parent class can be used in place of the parent class, it doesn't matter whether the port_object is a Port_A object or a Port_B object. The appropriate function for either Port_A or Port_B gets called depending on whether the message was a Message_A or Message_B message. The message gets written and everyone is happy, EXCEPT THE COMPILER. To set this up, class Port needs to know about Message_A class and Message_B class. Message_A and Message_B class need to know about type Port. Now imagine you have your include files. The header for class Message_A includes Port.h . The header for Port has to include Message_A.h . When compiling, one of the class definitions is going to come first and the definition for the other class is undefined. OUCH. WHERE AM I MISSING THE BOAT? This problem must have come up before. Is there a standard way for solving it? The only method I can figure out is to have two different header files for one of the classes. The one file has no function definitions in it. Therefore it does not need to include the header file for the other class. This header file can then be included by the other class without things breaking. Thanks in advance.