Problem with multiple definition

A

Antonio Rivas

Hello all.
I've got a problem of multiple definition in a program that at first
glance looks correct (I won't type the whole code, just the relevant one
and as examples since seems is a linkage problem):

main.cpp
========
using namespace std;
....

#include "myHeader.h" // This is the header that causes the problem

....

int main()
{
...

Function_in_myHeader();

...
}

myHeader.h
==========
#ifndef MYHEADER_H
#define MYHEADER_H

....

void Function_in_myHeader(void);

#endif

myHeader.cpp
============
....

#include "myHeader.h" // I suspect the problem is here

....

void Function_in_myHeader(void)
{
- do something -
}

As I said, this structure arises a multiple definition error of
Function_in_myHeader (also of other identifiers declared in myHeader.h)
The problem is that I need to include myHeader.h in myHeader.cpp because
when I remove it the compiler throws errors of undeclared identifiers.
I tried also compile the program embedding the line within #ifndef
#define #endif but the result is the same as if I remove the line: not
declared identifiers/functions errors arise.

I have no clue about what is wrong.
Btw, I use gcc with CodeBlocks frontend and the program is compiled as C++

Thank you in advanced.
 
R

[rob desbois]

As I said, this structure arises a multiple definition error of
Function_in_myHeader (also of other identifiers declared in myHeader.h)
The problem is that I need to include myHeader.h in myHeader.cpp because
when I remove it the compiler throws errors of undeclared identifiers.
I tried also compile the program embedding the line within #ifndef
#define #endif but the result is the same as if I remove the line: not
declared identifiers/functions errors arise.
Are you compiling myHeader.cpp into myHeader.o then linking that with
main.cpp?

If you have implemented (i.e. provided a function body for)
function_in_myHeader IN the header itself then the function will be
defined in myHeader.o and you will get a multiple definition error
when you then link that with main.cpp
Any help? If not can you post the contents of myHeader.h and the
makefile/commands you use to compile it.

--rob
 
A

Antonio Rivas

[rob desbois] escribió:
Are you compiling myHeader.cpp into myHeader.o then linking that with
main.cpp?
The compiler generates a myHeader.o so basically the answer is yes, the
main.cpp is linked after (i suppose).

I forgot mention (my fault) that the compiler points to main() as the
point in which the myHeader.cpp instances were declared for the first time.
If you have implemented (i.e. provided a function body for)
function_in_myHeader IN the header itself
No. The body of the functions are in myHeader.cpp
then the function will be
defined in myHeader.o and you will get a multiple definition error
when you then link that with main.cpp
Any help? If not can you post the contents of myHeader.h and the
makefile/commands you use to compile it.

--rob

Thank you, you helped me :)
As I was replying I realize that the problem was not the function itself
but a parameter that is passed that happens is an array of strings which
I declared and initialized in the .h file.
I just changed this array to myHeader.cpp and compiled and all multiple
declaration errors vanished :)

Thank you again --rob :)
 
T

tragomaskhalos

Hello all.

main.cpp
========
using namespace std;
...
#include "myHeader.h" // This is the header that causes the problem
...

An unrelated point - you want to get into the habit
of putting your "using namespace ..."s *after* your
#includes; you may save yourself hours of head-scratching
one day.
 
A

Antonio Rivas

tragomaskhalos escribió:
An unrelated point - you want to get into the habit
of putting your "using namespace ..."s *after* your
#includes; you may save yourself hours of head-scratching
one day.
I know, sorry, in the source code is correctly placed after not before :)
But thanx for the reminder :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top