views:

39

answers:

3

Hi All,

I am getting errors like:

FxMathFunctions.h: In function 'FxInt32 IMin(FxInt32, FxInt32)':

FxMathFunctions.h:13: error: redefinition of 'FxInt32 IMin(FxInt32, FxInt32)'

FxMathFunctions.h:15: error: 'FxInt32 IMin(FxInt32, FxInt32)' previously defined here

In FxMathFunctions.h I have:

11: struct FxPoint2d;
12:
13: inline FxInt32 IMin(FxInt32 i1,FxInt32 i2)
14: {
15:    if (i2 < i1) i1 = i2;
16:    return i1;
17: }

FxInt32 is defined in a header that I am including as:

typedef long                FxInt32;

I cant decide by the errors if it says that FxInt32 is being redefined or if the whole function is.

How do I solve this?

UPDATE I added the line numbers above.

A: 

Move the function definition into a .cpp file and just have the prototype in the .h file. Let the compiler worry about the optimization

Anders K.
@anders: To do this, I would not using inline in the .cpp, correct? and how would I change to put the prototype in the .h? The use of inline confuses me, dont I just remove it since I am moving the code into the .cpp?
Elliot
Yes, no need for inline in the .cpp file. The inline keyword is a directive telling the compiler to copy the whole function implementation instead of just calling the function. Just remove the inline, put FxInt32 IMin(FxInt32 i1,FxInt32 i2); in your header and the definition in the .cpp file and you are good to go. The typedef you should place in the header as well of course.
Anders K.
A: 

It's hard to say without knowing what's on lines 13 and 15 of FxMathFunctions.h. That said, keep in mind that C++ has a built-in std::min and std::max in <algorithm>, and they work for all comparable types.

bdonlan
@bdonlan - Thanks, let me look into these.
Elliot
Added links to the documentation for you
bdonlan
+1  A: 

It's saying the whole function is defined twice.

My psychic debugging powers tell me you are somehow recursively including that header, and that header doesn't have a proper guard against this happening. Thus the inline function is defined twice.

Terry Mahaffey
My psychic upvoting powers tell me you are probably right.
James McNellis
Weird - why would the reported line numbers be shifted then?
bdonlan
@Terry - I have a header guard in place and to be sure, I changed the name of it. #pragma once as well
Elliot