Header files

This is a discussion on Header files within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I have 2 header files in my project and one them defines a struct type that the other ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Header files

    Hey guys,

    I have 2 header files in my project and one them defines a struct type that the other header uses. If I include the first one in the second one, it thinks I'm tryign to redefine my struct. IE.
    1st header: containes a struct definition
    2nd header: has a function declaration that uses the strust

    do I have to define the struct again in the 2nd one or is there another way?

    If I just use the struct in the 2nd one, the function doesn't recognise it and gives me an undefined variable error.
    The headers are included in alphabetical order.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    20
    Why don't you make your struct a class, and then include the function as a method of that class?

    Just a thought from a noob.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I use the struct in another class. the function has to be called by the main separatelly. It's easier that way, I think. The function takes more argumetns then just the struct.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Add include guards to your header files. If you already have, post some code and the error.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    One sec, gotta read about include guards.....

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I always put include guards in. It saves a lot of hastle later.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    OK, I don't think I'm doing it right with the header guards. Here are the files:

    Original header:
    Code:
    /*
    *  Header File: EDFParse3.h
    *
    */
    
    # include <string.h>
    # include <stdlib.h>
    # include <ctype.h>
    # include<cstdio>
    # include <vector>
    # include <iostream>
    # include "stdafx.h"
      
    using namespace std;
    
    struct DataLine
    {
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    
    	DataLine()
    	{
    		Label=SFactor=SigBits=Bit30=Bit31=Max=Min=0;
    		memset(Param, 0, sizeof(Param));
    	};
    
    } ;
    
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    		std::vector < DataLine > vectDL;		
    	}; 
    
     /*#ifdef __cplusplus
     extern "C" {
     #endif */
    
    #ifndef EDFParse3_HPP
      
    #define EDFParse3_HPP 
    
    
    int FindWord(FILE *inp,char KeyWord[25], char *string);
    int GetWord(FILE *inp, char *string);
    void SkipWS(FILE *inp);
    void ReadDataSec(FILE * inp, FSAD &Section , int nExpected);
            //void print_Struct( struct DataLine sec );
            //int ReadSection(FILE * inp, struct DataLine Sec[], int nExpected);
    FILE* OpenFileEDF(FILE *out);
    
    
    #endif
    the header that uses the previous one:
    Code:
    # include <stdio.h>
    # include <math.h>
    # include <STDLIB.H>
    # include "EDParse3.hpp"
    
    # define BIT01 0X00000001
    # define BIT02 0X00000002
    # define BIT03 0X00000004
    # define DATAMASK18 0XFFFFC00
    
    #ifndef MakeArincAnalog_H 
    #define CreateWord_H 
    
    int MakeLabel(int number /*Input Octal label*/);
    int MakeArincWordAnalog(DataLine Line, float value,  int word);
    //float DecodeArincAnalog(int word, float scale);
    #endif
    and the main:
    Code:
    .............
    #include "BAFParse.h"
    #include "EDFParse3.hpp"
    #include "MakeArincAnalog.h"
    ....................
    one of the errors:
    Code:
    fatal error C1083: Cannot open include file: 'EDFParse3.hpp': No such file or directory
    MakeArincWordAnalog.cpp

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    The include guards should cover the entire header file. So
    #ifndef EDFParse3_HPP
    #define EDFParse3_HPP

    should be at the top of the file and the #endif should be at the bottom.

    Your error is probably because you named your header file EDFParse3.h instead of EDFParse3.hpp. Use .h or .hpp, but be consistent.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I changed the position of the header guards, and I renamed the actual file to EDFParse3.hpp and that did the trick.

    Thanks a lot guys,

    AS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 11:21 AM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 06:12 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21