Solved

Header Files

Posted on 1998-12-22
6
253 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now