Solved

Header Files

Posted on 1998-12-22
6
296 Views
Last Modified: 2010-04-02
I'm in Dialog mode. I have a main Dialog and several Dialogs that come off of the Main Dialog. I created a Header file and in the CMyProgram1Dlg (which is the Main Dialog) I used

#include "header.h"

Everything works fine.

The problem is when I want to #include the same Header file in one of the Sub Dialogs I get this error:

pddlg.obj : error LNK2005: "unsigned int  port" (?port@@3IA) already defined in Program1.obj

pddlg.obj : error LNK2005: "int __cdecl trans(unsigned int,unsigned int,int)" (?trans@@YAHIIH@Z) already defined in Program1.obj

If I don't #include the Header file I get errors that the variables are undefined.

What am I doing wrong and what do I have to do to correct it?

Thanks
Randy
0
Comment
Question by:randyg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1180868
In the header.h, you should only declare the variables and functions like the following.

extern unsigned int port;

int __cdecl trans(unsigned int,unsigned int,int);

Put the definitions in a source file, for example common.c like the following.

unsigned int port;

int __cdecl trans(unsigned int,unsigned int,int)
{
//...
}

Insert the source file into your project. Include the header file wherever you use the variables and functions.
0
 

Expert Comment

by:Gawande
ID: 1180869
Always put *.h code in # directive as follows

#ifndef _HEADER_FILE_NAME           // If this variable is not defined then only header file
#define _HEADER_FILE_NAME         // will get included. Header file should be loaded                                                             // only once.  So when you include this header file
// next time, the variable is already declared and hence, it will not give you any error

.



//// Header file contents

#endif // _HEADER_FILE_NAME
0
 

Expert Comment

by:Gawande
ID: 1180870
It seems that my commnets are destorted, please put your code in following way

#ifndef _FILE_NAME
#define _FILE_NAME

// Header file contents

#endif
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:chensu
ID: 1180871
Yes, you should do what Gawande suggests. However, it does not solve the problem randyg is facing.

It does solve the problem in the following example.

Header files: a.h, b.h, c.h
Source files: a.cpp

In the a.cpp,
#include "a.h"
#include "c.h"

In the a.h,
#include "b.h"
#include "c.h"

In this case, c.h is included twice.

It does not solve the problem in the following example.

Header files: a.h
Source files: a.cpp, b.cpp

In the a.h,
unsigned int port;

In the a.cpp,
#include "a.h"

In the b.cpp,
#include "a.h"

In this case, there are two global variables port with the same name.

To solve this problem,

In the a.h,
extern unsigned int port;

Add another source file c.cpp,
unsigned int port;
0
 

Expert Comment

by:talb
ID: 1180872
hi
try to save your header file in two diffrent names
then use it.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1180873
talb,

But the variables and the functions are the same. It will still cause the linking error.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question