Thread: Headers that use each other

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Headers that use each other

    Howdy,

    I have multiple header files and a source file. Each of the header files all #include each other (don't worry about why, you'll see below). The source file also #includes one of those headers.

    This is a breakdown of the files:
    connection.hpp - connection class
    headermap.hpp - declaration of functions that take CONNECTION class as an argument, and are called BY the CONNECTION class.
    stats.hpp - functions called by CONNECTION class and also take connection class as an argument.
    There are a lot more headers included that use parts of the CONNECTION class, those are just the main ones.

    So you can see that all three headers use parts of each other.

    connection.cpp - function definitions of the connection class methods.

    When I try to compile, I get errors on the headermap.hpp and stats.hpp files saying they don't know what CONNECTION is, but the class was already declared in connection.hpp (which they both #include).

    All of the headers have the appropriate #ifndefs and #defines, so they should only be compiled once. I've posted the code below so you can see it properly if you wish. If anyone can tell me whats gone wrong I would really appreciate it.

    NB- The code below will not compile anyway because theres a few other functions missing (I'm currently re-arranging everything).

    SWEBSConnection.Hpp
    -------------------------------------
    Code:
    #ifndef SWEBSCONNECTIONHPP
    #define SWEBSCONNECTIONHPP 1
    //--------------------------------------------------------------------------------
    //			SWEBSCONNECTION.hpp
    //          -------------------
    //          This is it. This is the most important part of the SWEBS source code,
    //          the code that handles all the requests. Everything the client will
    //          see comes through this class. 
    //
    //          When a request comes in, the main() function creates a new instance
    //          of the CONNECTION class, whos job it is to handle the connection. Main
    //          calls ReadRequest() to read in all the information from the client,
    //          and then calls HandleRequest(), which handles it.
    //
    //          This is how the CONNECTION class is created:
    //          CONNECTION::CONNECTION(
    //              int SFD_SET,                - Socket descriptor for this connection
    //              struct sockaddr_in,         - Sockaddr_in structure used
    //              OPTIONS Options,            - Options class, loaded in main
    //              STATS Stats);               - Stats class, also loaded in main
    //
    //--------------------------------------------------------------------------------																				
    //			INCLUDES
    //--------------------------------------------------------------------------------
    #include "SWEBSStats.hpp"
    #include "SWEBSHeaderMap.hpp"
    #include "SWEBSCGI.hpp"
    #include "SWEBSLog.hpp"
    #include "SWEBSSocket.hpp"
    #include "SWEBSUtilities.hpp"
    #include "SWEBSOptions.hpp"
    #include "SWEBSISAPI.hpp"
    #include <windows.h>
    #include <string>
    #include <winsock.h>
    #include <map>
    #include <sstream>
    #include <algorithm>
    #include <fstream>
    #include <stdio.h>
    #include <ctime>
    #include <httpext.h>
    #include <httpfilt.h>
    #include <header.h>
    
    using namespace std;
    
    //---------------------------------------------------------------------------------------------
    //			Connection class
    //---------------------------------------------------------------------------------------------
    class CONNECTION
    {
      public:
    	CONNECTION(int SFD_SET, struct sockaddr_in, OPTIONS Options, STATS Stats);	  // Constructor
    	CONNECTION();
    	
    	bool LogConnection();														  // Logs connection to the appropriate log
    	bool ReadRequest();														   	  // Reads the request and sets values
    	bool HandleRequest();														  // Handles the request
    
    	// Methods
    	bool SetFileType();															  // Sets if the file is a script or binary
    	bool IndexFolder();															  // Indexes the folder by listing all the files
    	bool SendText();															  // Sends the requested file if it is text
    	bool SendCGI();																  // Sends the requested file if it is a script
    	bool SendBinary();															  // Sends the requested file if it is binary
    	bool SendError();															  // Outputs the appropriate error code
    	bool SendISAPI();                                                             // Gets the ISAPI request
        string CalculateSize();													      // Outputs the file size
    	bool ModifiedSince(string Date);										      // Was the file modifed since...
    	bool UnModifiedSince(string Date);											  // Is the file Unmodified since...
    
    	// Properties
    	int SFD;																   	  // Socket descriptor of connection
    	struct sockaddr_in ClientAddress;											  // Client address structure
    	OPTIONS Options;															  // Options file
    	STATS SWEBSStats;															  // Stats file
    	VIRTUALHOST *ThisHost;														  // This virtual host host
        REQUEST_SPECIFIC_CGI CGIVariables;                                            // CGI Environment variables
    
    	string FullRequest;															  // The entire input from the client
    	string RequestType;															  // Type of request (POST, GET etc)
    	string FileRequested;														  // String folling GET
    	  string QueryString;														  // Anything after the '?' in the file
    	  string Extension;															  // Extension of file requested
    	  string RealFile;															  // Real path to file
    	  string RealFileDate;														  // Last Modification date of RealFile
    	string HTTPVersion;															  // HTTP version of the client
    	istringstream PostData;														  // Data supplied AFTER the double newline, for post requests
    
    	string Headers;																  // Headers to be sent with the file
    	map <string, bool> Accepts;													  // MIME types the client accepts
    	string UserAgent;															  // Browser used by the user
    	string HostRequested;														  // Host: from browser
    	string From;																  // From: value (email address normally)
    	string ConnectionType;														  // Connection: type (keep alive normally)
        string Referer;                                                               // Referer: file that refered the user to this document
    
    	string Date;																  // Date/time of this request
    	string ModifiedSinceStr;													  // IfModifiedSince string
    	string UnModifiedSinceStr;													  // IfUnModifiedSince string
    	bool UseModDate;															  // Do we use an If-Modified-Since
    	bool UseUnModDate;															  // Do we use the If-Unmodified-Since
    
    	int Status;																	  // Status code for request (404, 200 etc)
    	bool UseVH;																	  // Does the connection use a virtual host or a real one
    	bool IsFolder;																  // Is the file requested a folder or a file
    	bool IsBinary;																  // Is the file binary?
    	bool IsScript;																  // Is the file a script
    	bool IsIsapi;                                                                 // The file requested uses ISAPI
        bool IsAbsolute;															  // Did the client use an absolute address
    };
    
    //--------------------------------------------------------------------------------
    extern map<int, CONNECTION *> Connections;                                        // Map of all the connections
    
    //--------------------------------------------------------------------------------
    #endif
    SWEBSHeaderMap.Hpp
    --------------------------------
    Code:
    #ifndef SWEBSHEADERMAPHPP
    #define SWEBSHEADERMAPHPP 1
    //---------------------------------------------------------------------------------------------
    /*
    			SWEBSHEADERMAP.HPP
    			--------------
    			This file contains functions used for header mapping. Header mapping
    			is the process of mapping a header such as "Host:" to a function to
    			handle that header. These functions change the Connection argument 
    			depending on what headers are sent. Using a map its easy to disregard
    			headers we don't understand/support.
    			
    			Header mapping can't be used until the function HeaderMapInit has been
    			called, which is usually done by the main server program.
    			
    			The functions for this are in the library SWEBSHeadermap.lib 
    */
    //---------------------------------------------------------------------------------------------
    #include "SWEBSLog.hpp"
    #include "SWEBSStats.hpp"
    #include "SWEBSCGI.hpp"
    #include "SWEBSOptions.hpp"
    #include "SWEBSConnection.hpp"
    #include "SWEBSUtilities.hpp"
    #include "SWEBSSocket.hpp"
    #include "SWEBSISAPI.hpp"
    #include <sstream>
    
    using namespace std;
    
    //---------------------------------------------------------------------------------------------
    //			Function Definitions
    //---------------------------------------------------------------------------------------------
    extern bool SWEBS_hm_ACCEPT_CHARSET(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_ACCEPT_ENCODING(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_ACCEPT_LANGUAGE(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_AUTHORIZATION(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_CONNECTION(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_CONTENT_ENCODING(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_CONTENT_LANGUAGE(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_CONTENT_LENGTH(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_CONTENT_TYPE(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_FROM(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_IF_MODIFIED_SINCE(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_IF_NOT_MODIFIED_SINCE(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_HOST(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_REFERER(istringstream &IS, CONNECTION * Connection);
    extern bool SWEBS_hm_USER_AGENT(istringstream &IS, CONNECTION * Connection);
    
    typedef bool (*SWEBS_HM)(istringstream &IS, CONNECTION * Connection);
    
    extern bool HeaderMapInit();
    
    //---------------------------------------------------------------------------------------------
    //			MAP
    //---------------------------------------------------------------------------------------------
    extern HINSTANCE gSWEBS_headermapDLL;
    extern map <string, SWEBS_HM>HeaderMap;
    
    //---------------------------------------------------------------------------------------------
    #endif
    SWEBSStats.Hpp
    ------------------------
    Code:
    #ifndef SWEBSSTATS
    #define SWEBSSTATS 1
    //-------------------------------------------------------------------------------
    //			SWEBSSTATS.hpp
    //			-------------------
    //			This file contains functions and classes used when logging statistics.
    //
    //-------------------------------------------------------------------------------																				
    //			INCLUDES
    //-------------------------------------------------------------------------------
    #include <string>
    #include <map>
    #include "SWEBSLog.hpp"
    #include "SWEBSHeaderMap.hpp"
    #include "SWEBSCGI.hpp"
    #include "SWEBSOptions.hpp"
    #include "SWEBSConnection.hpp"
    #include "SWEBSUtilities.hpp"
    #include "SWEBSSocket.hpp"
    #include "SWEBSISAPI.hpp"
    
    using namespace std;
    //--------------------------------------------------------------------------------
    //			VHSTATS Class
    //--------------------------------------------------------------------------------
    class VHSTATS
    {
    public:
        VHSTATS();
        VHSTATS(VIRTUALHOST);                                                         // Stats on a per-virtualhost basis
        friend bool operator>(const VHSTATS lhs, const VHSTATS rhs);                  // Needed for use in map
        friend bool operator<(const VHSTATS lhs, const VHSTATS rhs);
        map <string, int> PageRequests;                                               // Page requests (per VH)
        unsigned long NumberOfRequests;                                               // Number of requests
        unsigned long BytesSent;                                                      // Bytes sent for this virtual host
        VIRTUALHOST ThisVH;                                                           // This virtual host
    };
    
    //--------------------------------------------------------------------------------
    //			STATS Class
    //--------------------------------------------------------------------------------
    class STATS
    {
      public:
        STATS();                                                                      // Constructor - must load the stats
        bool WriteStatsFile();                                                        // Writes all statistics to the stats file
        
        unsigned long NumberOfRequests;                                               // Number of connections served by the server
        unsigned long TotalNumberOfRequests;                                          // Total number of connections served
        unsigned long BytesSent;                                                      // Total number of bytes served
        unsigned long TotalBytesSent;                                                 // Total number of bytes sent for VH's and normal requests
        string LastRestart;                                                           // Last time the server was restarted
        map <string, int> PageRequests;                                               // Page requests
        map <VIRTUALHOST, VHSTATS> VirtualHosts;                                      // To keep stats on a per-virtualhost basis 
        map <int, string> PageRequestIndex;                                           // Index of pages requested
    };
    
    //--------------------------------------------------------------------------------
    //			Externals
    //--------------------------------------------------------------------------------
    extern bool CreateStatsThread(string File);										  // Creates the stats thread using FILE as the file to use 
    
    //--------------------------------------------------------------------------------
    #endif
    I won't post the whole of connection.cpp, just the top:

    SWEBSConnection.Cpp
    Code:
    //--------------------------------------------------------------------------------																				
    //			INCLUDES
    //--------------------------------------------------------------------------------
    #include "../Include/SWEBSConnection.hpp"
    #include <windows.h>
    #include <string>
    #include <winsock.h>
    #include <map>
    #include <sstream>
    #include <algorithm>
    #include <fstream>
    #include <stdio.h>
    #include <ctime>										
    #include <httpext.h>
    #include <httpfilt.h>
    #include <header.h>
    
    using namespace std;
    
    [...]
    ..define a bunch of functions
    Can anyone think of why it is causing problems? I get errors saying no one knows what CONNECTION is.

    BTW it's designed to compile as a .lib.

    If anyone can help I would really appreciate it, thanks! :D
    Last edited by nickname_changed; 10-03-2003 at 12:03 AM.

  2. #2
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Sorry, heres the errors:
    Code:
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSHeaderMap.hpp(34) : error C2061: syntax error : identifier 'CONNECTION'
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSHeaderMap.hpp(35) : error C2061: syntax error : identifier 'CONNECTION'
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSHeaderMap.hpp(36) : error C2061: syntax error : identifier 'CONNECTION'
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSConnection.hpp(115) : error C2059: syntax error : '>'
    The last one is a bit wierd, It refers to the line:
    extern map<int, CONNECTION *> Connections;
    I don't see why thats wrong...

  3. #3
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Why do you do such confusing things, better doing a single header which includes every others, it would makes things clear for everyone to debug...

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    So you think it would work if I made a file called "includes.hpp" and have that file #include EVERYTHING needed in the project, and have all the source and other headers #include that file? Would that work? I never thought of it that way...

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I don't say that it would work that way but it will be simplier to figure out what's happening. Besides, other headers don't have to include it, only sources.

  6. #6
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    lol, it did work actually, thanks heaps Lyx

  7. #7
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Hm... now its just being weird. The number of errors went from 102 to 7, so it seems to have worked.

    I have a class called VHSTATS, which looks like this:
    Code:
    class VHSTATS
    {
    public:
        VHSTATS();
        VHSTATS(VIRTUALHOST);                                                         // Stats on a per-virtualhost basis
        friend bool operator>(const VHSTATS lhs, const VHSTATS rhs);                  // Needed for use in map
        friend bool operator<(const VHSTATS lhs, const VHSTATS rhs);
        map <string, int> PageRequests;                                               // Page requests (per VH)
        unsigned long NumberOfRequests;                                               // Number of requests
        unsigned long BytesSent;                                                      // Bytes sent for this virtual host
        VIRTUALHOST ThisVH;                                                           // This virtual host
    };
    I also have a class called VIRTUALHOST (used BY VHSTATS). When I try to compile one of the errors I get is:
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSStats.hpp(23) : error C2460: 'VIRTUALHOST' : uses 'VHSTATS', which is being defined
    C:\SWEBS\SWEBSEngine\Source\../Include/SWEBSStats.hpp(20) : see declaration of 'VHSTATS'
    What does it mean its being 'defined'? At the moment ALL the files include "includes.hpp", which includes all the files. Whats going on? I need to get a book that teaches about this sort of thing!

  8. #8
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Show me the code of your VIRTUALHOST class.
    About buying a book, that is quite useless, you'd better try to figure it out by yourself. Yet, even if you think of yourself as a very good C++ programmer, you still have things to learn. ^^ Don't mean to offense you, I'm just like you, I learn on my own for about three years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-18-2005, 02:26 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  4. how do you handle all your headers?
    By mart_man00 in forum Linux Programming
    Replies: 0
    Last Post: 06-16-2003, 03:17 PM