-
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.
-
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.
-
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.
-
Add include guards to your header files. If you already have, post some code and the error.
-
One sec, gotta read about include guards.....
-
I always put include guards in. It saves a lot of hastle later.
-
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
-
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.
-
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.