inline and link errors

D

Dave

Hello all,

I have one method of a class calling another method (both private, though
that shouldn't matter). The method and its enclosing class are declared in
a .h file and the method is defined in a corresponding .cpp file. If I mark
the method as inline (either in the declaration alone, the definition alone
or both), I get a link error. If I do not mark the method as inline,
everything is fine. Can anybody think of a plausible explanation for this
behavior? My platform is VC++ 7.1.

Thanks,
Dave
 
V

Victor Bazarov

Dave said:
I have one method of a class calling another method (both private, though
that shouldn't matter). The method and its enclosing class are declared in
a .h file and the method is defined in a corresponding .cpp file. If I mark
the method as inline (either in the declaration alone, the definition alone
or both), I get a link error. If I do not mark the method as inline,
everything is fine. Can anybody think of a plausible explanation for this
behavior? My platform is VC++ 7.1.


If you declare the implementation inline and put it into a source file,
you will get errors when trying to call it from another source file.
If you put the implementation into a header but outside the class
definition and _not_ mark it inline, you will get errors if the header
is included into more than one source.

Why all the games? A simple thing to remember is "if a member function
is defined in the header, it needs to be declared 'inline', otherwise
it mustn't". Follow that rule and the worst you can do is declare it
'inline' when it's already inline (defined in the class definition).

Victor
 
A

Alan Johnson

Dave said:
Hello all,

I have one method of a class calling another method (both private, though
that shouldn't matter). The method and its enclosing class are declared in
a .h file and the method is defined in a corresponding .cpp file. If I mark
the method as inline (either in the declaration alone, the definition alone
or both), I get a link error. If I do not mark the method as inline,
everything is fine. Can anybody think of a plausible explanation for this
behavior? My platform is VC++ 7.1.

Thanks,
Dave

The standard states:

"An inline function shall be defined in every translation unit in which
it is used."

So, essentially, the answer to your question is, you can't do that. If
you are declaring a function as inline, you must arrange for its
definition to appear in each translation unit, rather than off in its
own translation unit as you are doing.

Alan
 
J

JKop

Dave posted:
Hello all,

I have one method of a class calling another method (both private,
though that shouldn't matter). The method and its enclosing class are
declared in a .h file and the method is defined in a corresponding .cpp
file. If I mark the method as inline (either in the declaration alone,
the definition alone or both), I get a link error. If I do not mark
the method as inline, everything is fine. Can anybody think of a
plausible explanation for this behavior? My platform is VC++ 7.1.

Thanks,
Dave


Think of "inline" as an abbreviation of "static inline".


static = internal linkage, ie. not visible outside of the current
translation unit.


-JKop
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top