Solved

Header Files

Posted on 1998-12-22
6
274 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

786 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