473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

inline and link errors

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
Jul 22 '05 #1
3 2068
Dave wrote:
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
Jul 22 '05 #2
Dave wrote:
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
Jul 22 '05 #3
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: A | last post by:
Hi, I'm having problems completing a project in C++. I have been using inline functions in some of my header files. I have only done so for simple functions that only have 1 statement (eg....
3
by: Peng Yu | last post by:
Hi, I'm trying to define inline_test::test() to be inline. But I always got errors. I know that if I define inline_test::test() in inline_test.h, there are no errors. But I still would rather...
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
1
by: kelvSYC | last post by:
I'm not sure if it violates standards or if it's just one of gcc's quirks, but on gcc with Mac OS X (at least with my installation), for some reason inline member functions that are not part of the...
5
by: Martin Vorbrodt | last post by:
so if i have this: // header.h class X { public: static int s = 1; }; i still need this:
13
by: jamihuq | last post by:
Can someone please tell me what I am doing wrong. I wrote this static inline function to test another function and I am getting errors which make no sense to me. thanks Jami /* test2.h */ ...
9
by: lucifer | last post by:
Hi I have a little problem regarding inline functions ,does declaring a function inside the class definition make it inline or not.
3
by: Rahul | last post by:
Hi Everyone, I have the following code, file1.cpp --------- #include <cstdio> inline int sample1()
5
by: sunil | last post by:
Hello, I have a class deriving from a class that provides ability to serialize/deserialize objects over the network. There are two classes Requests (sent from client to server) Response(sent from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.