Don't know how to link ".h"s and ".cpp"s in a Code::Blocks project .

HI!
I am writing a program that contains some classes and for that reason I need lots ".h"s and ".cpp"s .
But an error occurs : redeclaration of a function .
This is the whole Code::Blocks project .
There's not much coding in this project , I just wrote it for test .
Please download it (coders who have/use Code::Blocks) and say what part am I doing wrong ?

http://s1.picofile.com/file/7883659458/RedeclarationTest.rar.html

in the page above , almost in the middle you can see some social network icons , right under it there is a Rea-&-Grey button with a down arrow on the right .
click on it and the download will start .


I'd appreciate any help .
Thanks!
Would you mind including a copy-paste of the specific error?
It should have line numbers referencing specific files.
Last edited on
This is the whole error copied :
||=== RedeclarationTest, Debug ===|
obj\Debug\Person.o||In function `Z4itosi':|
C:\Users\Ardeshir81\Desktop\RedeclarationTest\AdditionalFuncs.h|9|multiple definition of `itos(int)'|
obj\Debug\Date.o:C:\Users\Ardeshir81\Desktop\RedeclarationTest\AdditionalFuncs.h|9|first defined here|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|


And as it is complicated , here is the summary :
in function "Z4itosi" : //I haven't declared a function with this name

multiple definition of "itos (int)" (line 9 , file AdditionalFuncs.h)
First defined here : line 9 , AdditionalFuncs.h


As I understand it is including one file (maybe AdditionalFuncs.h) twice .

Thanks!
hi.
i downloaded the file , but looks like its corrupted (maybe i'm missing something).

anyway, my best guess would be the include guards, try this:
in the AdditionalFuncs.h, do the following:
1
2
3
4
5
6
#ifndef ADDITIONALFUNCS_H
#define ADDITIONALFUNCS_H

//  place you code here.

#endif 


do the same with the other files changing the MACRO name for each file.
this will deny any file to be included more than once.

this might clear something up:
http://www.cplusplus.com/forum/general/71787/#msg382769
¿are you defining a function in a header?
@rechard3: His include files already have that. (I looked at the includes when I was at work before the rar file was broken)

@ardeshir81: It looks as if you are compiling date.o and person.o
And both these .o files have a definition of itos(int)

obj\Debug\Person.o||In function `Z4itosi':|

This is showing the person.o file, and even though you did not name the function Z4itosi this is still your function. .o files use strange names that the compiler gives, and can be even stranger if the code is obfuscated. This line tells you where the problem is.

C:\Users\Ardeshir81\Desktop\RedeclarationTest\AdditionalFuncs.h|9|multiple definition of `itos(int)'|
This line tells you what the problem is.

obj\Debug\Date.o:C:\Users\Ardeshir81\Desktop\RedeclarationTest\AdditionalFuncs.h|9|first defined here|

The function is first defined in Date.o whose source came from here:
C:\Users\Ardeshir81\Desktop\RedeclarationTest\AdditionalFuncs.h|9

This is another line to tell you what the problem is. Since the problem is a multiple definition of a function the compiler needs to at least tell you both the locations.

So it would look as if you are compiling each of these .o files separately and then trying to combine them to produce the exe? I am just guessing on that, but if that is the case then this function needs to be defined in only one .cpp file. When it is only in one .cpp file it should only end up in one .o file.
I am also guessing that the function is defined in Date.cpp and Person.cpp.
I would do a lot less guessing but the archive is damaged on the download site.
Last edited on
Thank Everyone , specially @Kevin for help .
Actually the file isn't broken , I compressed it wit WinRAR 5 (It's out) , I think you should download it .
By the way I recompressed it in zip format :
http://s1.picofile.com/file/7884584729/RedeclarationTest.zip.html
Well that explains why it worked while I was at work, because I downloaded winrar 5 on my work computer.
In AdditionalFuncs.h you define a function in a header file, so ne555 guessed the problem exactly.
If you had an AdditionalFuncs.cpp, you would want the definition of itos to go in there.

A personal preference of mine is just to use stringstreams to convert numbers to strings. This would work with negative numbers and fractional numbers as well as integers.
http://ideone.com/3LgDiD
Topic archived. No new replies allowed.