GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-May-2005, 03:18
mercury mercury is offline
New Member
 
Join Date: May 2005
Posts: 4
mercury is on a distinguished road

Writing good header files


Hello Guys,
I am not a really got in writing header files. My problem is that no matter how I write the header file, I get the "Multiple definition of..." errors. Here is an example of my header:

CPP / C++ / C Code:
#ifndef _h_ftdi_
#define _h_ftdi_

CPP / C++ / C Code:
#include "windows.h"
#include ...

CPP / C++ / C Code:
// declaration of a function
int LoadDll(void);

// pointer to the dll
HINSTANCE h_module;

// FT_ListDevices
typedef FT_STATUS(*PtrFT_ListDevices)(PVOID,PVOID,DWORD);
PtrFT_ListDevices pFT_ListDevices;

#endif

If I include this header file in my cpp files, I get "Multiple definition of h_module" and "Multiple definition of pFT_ListDevices" errors. Any idea what I'm doing wrong?

Thanks and best regards to you all.
George
Last edited by LuciWiz : 15-May-2005 at 07:22. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 15-May-2005, 06:34
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by mercury
Hello Guys,
I am not a really got in writing header files. My problem is that no matter how I write the header file, I get the "Multiple definition of..." errors.
... header code removed
If I include this header file in my cpp files, I get "Multiple definition of h_module" and "Multiple definition of pFT_ListDevices" errors. Any idea what I'm doing wrong?

Hello and Welcome George. How are you using h_module and pFT_ListDevices elsewhere in you program? When you have the variable declaration in your header like you have you would most likely need to declare it in another file (such as your main.cpp) something like the following so you could use it.
CPP / C++ / C Code:
// pointer to the dll declared in header
extern HINSTANCE h_module;

That way, the compiler knows how to make sense of it all. That is of course, just a guess based on what you have posted. I have run across this quite often using FLTK's UI builder Fluid since it makes heavy use of externs when it generates it's header code. You can only declare a variable once (as long as it really is the same variable or function) and using extern says, "I need to let the compiler know what this actually is, I need to use it in this section of code, go find it elsewhere." Just be sure extern is the ONLY difference in the two lines of code (same type or return value) or you will get an error.

I hope this helps, if not (or if my info is wrong) someone is bound to drop their two cents into the bucket.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 15-May-2005, 10:41
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by cable_guy_67
When you have the variable declaration in your header like you have you would most likely need to declare it in another file (such as your main.cpp) something like the following so you could use it.
That is exactly what is going on. You are creating the variable in the header which means every time you use the header you make another copy of the variable.

The header should contain externs and the variable is defined in a code file as cable_guy said.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to Text Files DesperateCry C Programming Language 2 19-Apr-2005 07:09
Reading and writing binary files in certain format Dream86 C++ Forum 10 06-Aug-2004 10:38
writing to 6 different files kilgortrout C++ Forum 5 20-May-2004 17:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.