Hi,


I get the error lnk2005 multiple defined symbol, because I have
non-member functions defined in a header file, that is included in
multiple compilation units.

If they were normal functions I'd make them static member functions. But
they're operators.

Is there a solution for this problem? Is is a MVCPP problem or is it a
c++ issue?

thanks
Ingo

Re: LNK2005 by David

David
Sun Feb 27 17:49:09 CST 2005

Ingo Nolden wrote:
> Hi,
>
>
> I get the error lnk2005 multiple defined symbol, because I have
> non-member functions defined in a header file, that is included in
> multiple compilation units.
>
> If they were normal functions I'd make them static member functions. But
> they're operators.
>
> Is there a solution for this problem? Is is a MVCPP problem or is it a
> c++ issue?
>
> thanks
> Ingo

Ingo:

Make them inline functions. When you define member functions inside a
class then they are automatically treated as inline; that's why you can
do that. If you move the member function definition outside the class
definition (but still in the header file) then you have to use the
inline keyword there also.

This is not a VC issue.

HTH,

David Wilkinson


Re: LNK2005 by Larry

Larry
Sun Feb 27 18:08:51 CST 2005

"David Wilkinson" <no-reply@effisols.com> wrote in message
news:egZ0pbSHFHA.3196@TK2MSFTNGP15.phx.gbl...
> Ingo Nolden wrote:
...
>> I get the error lnk2005 multiple defined symbol, because I have non-member functions defined in a header file, that is included
>> in multiple compilation units.
>>
>> If they were normal functions I'd make them static member functions. But they're operators.
>>
>> Is there a solution for this problem? Is is a MVCPP problem or is it a c++ issue?
...
> Make them inline functions. When you define member functions inside a class then they are automatically treated as inline; that's
> why you can do that. If you move the member function definition outside the class definition (but still in the header file) then
> you have to use the inline keyword there also.

The more traditional approach is to define the member
functions in a single translation unit, so they show up in
only one .obj file. This ensures that you get only one
copy of the code for each member function. (Well,
at least in a practical sense. An implementation could,
theoretically, effect 'inline' without being told to do so.)

Mr. Wilkinson's solution may be right for you, but you
should be aware that code bloat can result from use of
inlining in an inappropriate way.

--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.



Re: LNK2005 by Ingo

Ingo
Mon Feb 28 02:43:00 CST 2005

Larry Brasfield wrote:
> "David Wilkinson" <no-reply@effisols.com> wrote in message
> news:egZ0pbSHFHA.3196@TK2MSFTNGP15.phx.gbl...
>
>>Ingo Nolden wrote:
>
> ...
>
>>>I get the error lnk2005 multiple defined symbol, because I have non-member functions defined in a header file, that is included
>>>in multiple compilation units.
>>>
>>>If they were normal functions I'd make them static member functions. But they're operators.
>>>
>>>Is there a solution for this problem? Is is a MVCPP problem or is it a c++ issue?
>
> ...
>
>>Make them inline functions. When you define member functions inside a class then they are automatically treated as inline; that's
>>why you can do that. If you move the member function definition outside the class definition (but still in the header file) then
>>you have to use the inline keyword there also.
>
>
> The more traditional approach is to define the member
> functions in a single translation unit, so they show up in
> only one .obj file. This ensures that you get only one
> copy of the code for each member function. (Well,
> at least in a practical sense. An implementation could,
> theoretically, effect 'inline' without being told to do so.)
>
> Mr. Wilkinson's solution may be right for you, but you
> should be aware that code bloat can result from use of
> inlining in an inappropriate way.
>

Ok, I am not sure wether you noticed that I was talking about non-member
functions. Anyway, in a small test project, switching to inline did the
trick. Actually this is what I want, because the functions are quite
small I don't worry about code bloat

One of the actual functions is as follows:

inline Ref<String> S( const char* left )
{
return Ref<String>( new String( left ) );
}

It makes a String object from a const char* and returns a smart pointer
to it. I am sure that it is more syntactical sugar and code bloat is not
an issue.

However, the problem didn't disappear for this function.
May that be related to the fact, that I link a lib into the project that
also contains the function definition. Does a lib behave differently?

Ingo


Re: LNK2005 by shaileshdayani

shaileshdayani
Fri Mar 11 17:59:16 CST 2005


Try defining function as static


-
shaileshdayan
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------