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
Solved

Header Files

Posted on 1998-12-22
6
284 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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