same function name in different files

M

Martijn

Hi,

I have come unto a situation where two source files of one project contain
similar functions with the same name (it reads settings from a file and thus
named FetchSettings).

When I compile this project, it complains about redefining FetchSettings.
What can I do to avoid this behaviour? Declare it static, maybe?!?

Although renaming the functions is definitaly an option, I am wondering how
to solve this issue. Thanks for any input,
 
J

Jens.Toerring

Martijn said:
I have come unto a situation where two source files of one project contain
similar functions with the same name (it reads settings from a file and thus
named FetchSettings).
When I compile this project, it complains about redefining FetchSettings.
What can I do to avoid this behaviour? Declare it static, maybe?!?

If the functions are only used within the file they are defined in, yes,
declaring them static should resolve the problem.

Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
R

Richard Heathfield

Martijn said:
Hi,

I have come unto a situation where two source files of one project contain
similar functions with the same name (it reads settings from a file and
thus named FetchSettings).

When I compile this project, it complains about redefining FetchSettings.
What can I do to avoid this behaviour? Declare it static, maybe?!?

That's one option, but it makes a lot of sense to rename one of the
functions. If they have distinct prototypes, it'll be a lot easier to sort
out the mess.

Do check that they are two separate functions (different functionality); it
could be that one of them is just a slip of the mouse.
Although renaming the functions is definitaly an option, I am wondering
how
to solve this issue. Thanks for any input,

If you don't want to rename and static is an option, then use it, I guess.
It may not be an option if the functions are each called from more than one
translation unit.
 
C

CBFalconer

Richard said:
That's one option, but it makes a lot of sense to rename one of
the functions. If they have distinct prototypes, it'll be a lot
easier to sort out the mess.

Do check that they are two separate functions (different
functionality); it could be that one of them is just a slip of
the mouse.


If you don't want to rename and static is an option, then use it,
I guess. It may not be an option if the functions are each called
from more than one translation unit.

Whether or not he renames, the OP should probably mark them both
as static, and check that neither of them are mentioned in the
associated header file. As a general rule each source file should
declare all functions as static except those exported via the
header file (and main).

'static' is a very useful word for anything visible in file
scope. It keeps those items from being interfered with by
external entities, and from interfering with anything outside the
immediate compilation unit. When you see a static object you
immediately know that it is not affected by anything in any other
file. (Exception: if a pointer to it is passed out, which you can
detect in that source file alone.)
 

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
474,082
Messages
2,570,589
Members
47,211
Latest member
Shamestone

Latest Threads

Top